Thread: Is this code safe?

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367

    Is this code safe?

    Hi,

    I have created a basic function which returns all the ip addresses when you pass in the name of the server and port number. My question is, is the code safe?

    sIpAddress is created on the stack, and then assigned the ip from the return of the inet_ntoa function. This string is then pushed onto the list. However, as far I as i am aware, the list probaly uses ptrs to the type of object it is pointing to. So after this function returns, wont the memory created within the stack frame be destroyed?, i.e. string sIpAddress.....

    The function actually works, and returns three ip addresses if you pass in www.google.com. The results were:

    66.102.9.104
    66.102.9.99
    66.102.9.147

    Any advice welcome, thanks.

    Code:
    list <string> GetHostIp(string sName, string sPort)
    {
      list <string> listHostAddresses;
      string sIpAddress;
    	
      addrinfo aiHints;
      addrinfo *aiList = NULL;
      sockaddr_in *hostinfo;
    
      memset(&aiHints, 0, sizeof(aiHints));
      aiHints.ai_family = AF_INET;
      aiHints.ai_socktype = SOCK_STREAM;
      aiHints.ai_protocol = IPPROTO_TCP;
    
      if(getaddrinfo(sName.c_str(), sPort.c_str(), &aiHints, &aiList)) ProcessError(ERR_IP_ADDRESS, false);
    	
      for(addrinfo *i = aiList; i; i = i->ai_next) 
      {
        hostinfo = (sockaddr_in *)i->ai_addr;
    		
        sIpAddress = string(inet_ntoa(hostinfo->sin_addr));
        listHostAddresses.push_back(sIpAddress);
      }
      
      return listHostAddresses;
    }
    Be a leader and not a follower.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    That's where the string copy constructor comes into play. Whenever a string object gets pushed onto the list, a copy of the string is what actually gets stored (no pointer). When its time to exit the function and return, whatever list<string> object gets assigned the return value from this function will get a copy of the contents of listHostAddresses thank's to the list containers own copy constructor.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    The list will copy the values, the push_back is safe.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  4. #4
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    Ok. So say I was to dynamically allocate memory for a particular object, and pushing them onto the list; would I have to programatically traverse the list and explicitly delete all the items before the list goes out of scope?
    Be a leader and not a follower.

  5. #5
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    If your list is of type pointer and you have allocated memory for that pointer, then you will have to transverse the list to delete all the memory. If you use a non-pointer, it will copy the data and whatever the original memory is will have to be handled just after the assignment to the list.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by subdene
    Ok. So say I was to dynamically allocate memory for a particular object, and pushing them onto the list; would I have to programatically traverse the list and explicitly delete all the items before the list goes out of scope?
    Eventually you will have to delete the memory, but only once. If you are going to return a container of pointers to dynamically allocated memory from a function, you would do so in the calling function when you were done with whatever you needed rather than in the called function.:

    Code:
    list<string*> bar()
    {
        list<string*> strList;
        strList.push_back( new string("Hello") );
        strList.push_back( new string("World") );
    
        // Don't need to delete the memory here since we aren't done with it yet
    
        return strList;
    }
    
    void foo()
    {
        list<string*> strList = bar();
    
        for( list<string*>::iterator it = strList.begin(); it != strList.end(); ++it )
        {
            cout << **it << endl;
            // Now we should delete the memory since we are done with it.
            delete *it;
        }
    
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bjarne's exception safe sample
    By George2 in forum C++ Programming
    Replies: 13
    Last Post: 12-28-2007, 05:38 PM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM