Is this code safe?

This is a discussion on Is this code safe? within the C++ Programming forums, part of the General Programming Boards category; Hi,
I have created a basic function which returns all the ip addresses when you pass in the name of ...

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:

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

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?

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.

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.:

"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