Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > typecasting std::string to const void *

Reply
Thread Tools

typecasting std::string to const void *

 
 
vivekian
Guest
Posts: n/a
 
      03-21-2006
Hi,

This is the part of the code am trying to compile to :

void Server::respondToClient ( std::string response )
{
....
....
if ((numbytes = sendto ( sockFd_ , response , sizeof(response) , 0,
(struct sockaddr *)&clientAddr, sizeof (clientAddr))) == -1){
perror ("sendto");
exit (1) ;
}
....
}

This is the function prototype :
ssize_t sendto(int socket, const void *message, size_t length,
int flags, const struct sockaddr *dest_addr,
socklen_t dest_len);

Not sure how to go about typecasting the 'std::string' to 'const void
*' .

Thanks in advance ,
vivekian

 
Reply With Quote
 
 
 
 
Jakob Bieling
Guest
Posts: n/a
 
      03-21-2006
vivekian <(E-Mail Removed)> wrote:

> This is the part of the code am trying to compile to :
>
> void Server::respondToClient ( std::string response )


You might want to consider passing a const reference to avoid
unnecessary copying:

void Server::respondToClient (std::string const& response)

> {
> ...
> ...
> if ((numbytes = sendto ( sockFd_ , response , sizeof(response) , 0,
> (struct sockaddr *)&clientAddr, sizeof (clientAddr))) == -1){
> perror ("sendto");
> exit (1) ;
> }
> ....
> }
>
> This is the function prototype :
> ssize_t sendto(int socket, const void *message, size_t length,
> int flags, const struct sockaddr *dest_addr,
> socklen_t dest_len);
>
> Not sure how to go about typecasting the 'std::string' to 'const void
> *' .


You do not. You pass the data of the string and the length:

sendto (sockFd_, response.data (), response.length (), ...

hth
--
jb

(reply address in rot13, unscramble first)


 
Reply With Quote
 
 
 
 
TB
Guest
Posts: n/a
 
      03-21-2006
vivekian skrev:
> Hi,
>
> This is the part of the code am trying to compile to :
>
> void Server::respondToClient ( std::string response )
> {
> ...
> ...
> if ((numbytes = sendto ( sockFd_ , response , sizeof(response) , 0,


if ((sendto( sockFd_, response.c_str(), response.size(), 0,

> (struct sockaddr *)&clientAddr, sizeof (clientAddr))) == -1){
> perror ("sendto");
> exit (1) ;
> }
> ....
> }
>
> This is the function prototype :
> ssize_t sendto(int socket, const void *message, size_t length,
> int flags, const struct sockaddr *dest_addr,
> socklen_t dest_len);
>
> Not sure how to go about typecasting the 'std::string' to 'const void
> *' .
>
> Thanks in advance ,
> vivekian
>


--
TB @ SWEDEN
 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      03-21-2006

vivekian wrote:

> Hi,
>
> This is the part of the code am trying to compile to :
>
> void Server::respondToClient ( std::string response )
> {
> ...
> ...
> if ((numbytes = sendto ( sockFd_ , response , sizeof(response) , 0,
> (struct sockaddr *)&clientAddr, sizeof (clientAddr))) == -1){
> perror ("sendto");
> exit (1) ;
> }
> ....
> }
>
> This is the function prototype :
> ssize_t sendto(int socket, const void *message, size_t length,
> int flags, const struct sockaddr *dest_addr,
> socklen_t dest_len);
>
> Not sure how to go about typecasting the 'std::string' to 'const void
> *' .


To add a general point to the answers you've already got: the compiler
is your friend. Typecasting is not for situations where you are not
sure how to go about something. Using a cast amounts to silencing the
compiler and overruling its understanding of the language rules.
Therefore, you should only cast when you know exactly what you are
doing and why, in that particular case, you know better than the
compiler.

Gavin Deane

 
Reply With Quote
 
vivekian
Guest
Posts: n/a
 
      03-22-2006

Jakob Bieling wrote:
> vivekian <(E-Mail Removed)> wrote:
>
> > This is the part of the code am trying to compile to :
> >
> > void Server::respondToClient ( std::string response )

>
> You might want to consider passing a const reference to avoid
> unnecessary copying:
>
> void Server::respondToClient (std::string const& response)
>
> > {
> > ...
> > ...
> > if ((numbytes = sendto ( sockFd_ , response , sizeof(response) , 0,
> > (struct sockaddr *)&clientAddr, sizeof (clientAddr))) == -1){
> > perror ("sendto");
> > exit (1) ;
> > }
> > ....
> > }
> >
> > This is the function prototype :
> > ssize_t sendto(int socket, const void *message, size_t length,
> > int flags, const struct sockaddr *dest_addr,
> > socklen_t dest_len);
> >
> > Not sure how to go about typecasting the 'std::string' to 'const void
> > *' .

>
> You do not. You pass the data of the string and the length:
>
> sendto (sockFd_, response.data (), response.length (), ...
>


This works . But when passing a const reference , end up getting a
segmentation fault. After some debugging it seems , it occurs during '
response.length()' . Though the program works by pass by value , would
still like to discover the cause of the segmentation fault.

thanks .

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
what is the difference, void func(void) and void fucn() noblesantosh@yahoo.com C Programming 5 07-22-2005 04:38 PM
"void Method()" vs "void Method(void)" Ollej Reemt C++ 7 04-22-2005 03:47 AM
`void **' revisited: void *pop(void **root) Stig Brautaset C Programming 15 10-28-2003 09:03 AM



Advertisments