typecasting std::string to const void *

Discussion in 'C++' started by vivekian, Mar 21, 2006.

  1. vivekian

    vivekian Guest

    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
     
    vivekian, Mar 21, 2006
    #1
    1. Advertising

  2. vivekian <> 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)
     
    Jakob Bieling, Mar 21, 2006
    #2
    1. Advertising

  3. vivekian

    TB Guest

    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
     
    TB, Mar 21, 2006
    #3
  4. vivekian

    Gavin Deane Guest

    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
     
    Gavin Deane, Mar 21, 2006
    #4
  5. vivekian

    vivekian Guest

    Jakob Bieling wrote:
    > vivekian <> 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 .
     
    vivekian, Mar 22, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    606
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    843
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    886
    S.Tobias
    Jul 22, 2005
  4. Javier
    Replies:
    2
    Views:
    628
    James Kanze
    Sep 4, 2007
  5. 0m
    Replies:
    26
    Views:
    1,174
    Tim Rentsch
    Nov 10, 2008
Loading...

Share This Page