Skip to main content
GameDev.net gamedev.net
🔒 Locked

stl question

Started by denethor Nov 30, 2001 at 12:10 AM 1 replies 300+ views
Original Post
denethor
denethor
Say I do this: ------------------ list clist; clist.push_back("hello"); -------------- "hello" will store in the linked list ok, but is it overwriting memory or does list handle initialization of the pointer?
Kwizatz
Kwizatz
shouldnt it be:
  
list<string> clist;
clist.push_back("hello");


I know deque handles the initialization of the pointer, if you were to feed it a string variable, it will create a new instance and copy the contents over, it does not just keep track of pointers . . .

but then if you say instead:
  
list<string *> clist;

//next line should look like


clist.push_back(new string("hello"));

and you should make sure you delete it before poping it too, its all really a matter of what type of variable you are using



Edited by - kwizatz on November 30, 2001 1:33:40 AM

Edited by - kwizatz on November 30, 2001 1:34:47 AM
Fruny
Fruny
if it''s a list of char*, it''s just copying the pointer (to the constant chain "hello" ). If it''s a list of std::string, it will construct a string object from the pointer.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.