typecasting std::string to const void *

V

vivekian

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
 
J

Jakob Bieling

vivekian said:
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
 
T

TB

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,
 
G

Gavin Deane

vivekian said:
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
 
V

vivekian

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

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


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 .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top