Cannot pass char * array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Xx r3negade
    New Member
    • Apr 2008
    • 39

    Cannot pass char * array

    EDIT: Title is a misnomer, it's not an array it's a pointer

    I have a struct that I use to pass arguments to a function referenced by pthread_create( ). It looks like this:

    Code:
    struct __tryConnect_args
    {
        int sock;
        char * hoststr;
        unsigned int port;
        int proto;
    };
    My problem is with the char * hoststr member of the struct. Take a look at this code:

    Code:
    char * hostString;
    hostString = dnsLookup(argv[1], AF_INET); /* prototype : char * dnsLookup(char *, int) */
    
    printf("Connecting to %s\n", hostString);
    
    for (i = 0; i < maxConns; i++)
    {
            connArgs[i].sock = sock;
            connArgs[i].hoststr = hostString;
            connArgs[i].proto = PF_INET;
            connArgs[i].port = port;
    
            pthread_create( &connThreads[i],
                                     NULL,
                                     &__tryConnect,
                                     (void *) &connArgs[i] );
    }
    When __tryConnect() is executed, "sock", "proto", and "port" are passed correctly, but "hoststr" becomes gibberish.

    __tryConnect() looks like this:

    Code:
    void * __tryConnect(void * args)
    {
        struct __tryConnect_args * argsStruct;
        argsStruct = (struct __tryConnect_args *) args;
        printf("args: hoststr: %s\nport: %d\n", argsStruct->hoststr, argsStruct->port);
    }
    The program's output looks like this:
    Code:
    Getting address info for google.com
    Connecting to 209.85.171.100
    args: hoststr: �        �@
    port: 80
    What is happening with hoststr?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    What happens if you make a copy of the char* string right after dnsLookup() has returned it to you? It seems as if the buffer returned by that function is reused for other purposes ...

    kind regards,

    Jos

    Comment

    • Xx r3negade
      New Member
      • Apr 2008
      • 39

      #3
      OK, that works. I don't know what's happening with hoststr, though...the only two times that it's used in the program are the ones shown in the code. Whatever, it works though. Thanks.

      EDIT: On a side note, if I replace the code in dnsLookup() so it looks like this:
      Code:
      char * dnsLookup(char * hoststr, unsigned int proto)
      {
         return "testing";
      }
      everything works fine, without having to copy the character buffer. So the problem lies in my dnsLookup() function...

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Originally posted by Xx r3negade
        OK, that works. I don't know what's happening with hoststr, though...the only two times that it's used in the program are the ones shown in the code. Whatever, it works though. Thanks.
        Nothing happens to hoststr (ie, the pointer). The problem is with the string being pointed at.

        Function dnsLookup apparently constructs the string in a static buffer and returns a pointer to that buffer. Apparently some other function (perhaps pthread_connect ) uses that same buffer as a scratchpad, thereby corrupting the string that you expect hoststr to point to.

        You should always be suspicious of functions that return a pointer: what is the lifetime of the buffer being pointed to? There are two ways you can be burned: an interposed function call might corrupt the buffer; or "simultaneo us" calls to the same function by two threads might leave both threads pointing to the same buffer.

        Best is when a function copies such information into a buffer you provide as an argument; second best is for you to copy information from a "built-in" buffer into one of your own; third best is a function that mallocs the necessary buffer (but don't forget to free it!).

        Comment

        Working...