About the STL function "erase"

Started by
3 comments, last by Gandalf 23 years, 1 month ago
Lets say I have a vector element in a structure which contains a pointer to a char (char* text). I allocate memory for "text" somewhere in the program. Later when I dosn´t need the text buffer I can either call erase on the element ( a STL vector function) or call delete[] on the text pointer. Which approach is correct? Does the STL function release the memory for me or is it neccessery to call delete[] before (the program crash when I try to do that). Thanks, Gandalf the Black
Gandalf the Black
Advertisement
You left a few details out ... are you suggesting this?

your have a struture which contains a vector of char pointers? ... like this:

  struct MyStructType  {  std::vector<char*> text;  }  


of that you have a vector of structures that contain char pointers ... like this:

  struct MyStructType2  {  char *text;  }std::vector<MyStructType2> myVector;  


in the general sense, an STL container calls the destructors on elements it erases ... but that means this:

  std::vector<MyClass> vector1;std::vector<MyClass*> vector2;  


the elements of vector1 will be destroyed properly ... the elements of vector2 (the pointers themselves) will be destroyed .. but the items they point to will not ... so if you allocated the memory somewhere ... and are not deleting it elsewhere ... it''s getting leaked.

understand?

and in the case of having structures with pointers ... IF the structure is supposed to OWN the memory (meaning it has the responsibility of deleting it) ... then you must write a destructor for you structure ... which deletes the buffer. Assuming you do this:

  struct MyStruct3  {  ~MyStruct3(void) {delete[] text;}  char *text;  }  


then your class properly handles it''s ownership of the buffer (assuming it was allocated correctly using new[] and assigned into the text variable correctly). But you still have to be sure you delete your MyStruct3 variables ... so in the following

  std::vector<MyStruct3> items;std::vector<MyStruct3*> itemRefs;  


the items variable would handle the ownership issues ... but itemRefs leaves the deletion of the items to someone else ...

so ... if you read this and your eyes glaze over ... post a more detailed and specific question ... I''ll do what i can.

Good Luck
Thank you!

That was a good answer... I have struct to display a sentence of text...

struct TEXT{   char *text;   int ypos;   COLORREF color;};  


...then I have a global vector variable...

std::vector  g_TextVector;  


At one point in my program I want to add a text string to the vector...

TEXT t;t = new char[strlen("hello")];strcpy(t.text, "hello");t.ypos = y;t.color = color[c];g_TextVector.push_back(t);    


... and at another point in the code I wan´t to delete a specific string...

std::vector  ::iterator ob = g_TextVector.begin();   while(ob!=g_TextVector.end()){   if(ob->color == 0x0000FF00)   {      // delete[] ob->text;      g_TextVector.erase(ob);      break;   }   ob++;}   


BUT When I try to delete[] the memory my program crash!

Thanks,

Gandalf the Black


Edited by - Gandalf on February 19, 2001 4:11:27 PM
Gandalf the Black
Here's the part of the code that's the problem:
TEXT t;t = new char[strlen("hello")];strcpy(t.text, "hello");t.ypos = y;t.color = color[c];g_TextVector.push_back(t); 


t isn't a char*, it's a local TEXT object. It already has memory allocated to it on the stack holding the contents of an uninitialized TEXT structure. I'm not really sure if the line "t = new char[strlen("hello")];" will compile--you're assigning new memory to an object that isn't even a pointer.

This is what you should have
TEXT t;t.text = new char[strlen ("hello")+1]; // need +1 for null charstrcpy (t.text, "hello");t.ypos = y;t.color = color[c];g_TextVector.push_back(t);  


What you have for erasing the data should work now when you uncomment the delete [] ob->text line. vector::erase will erase the TEXT entry from the container. It will also call the destructor for TEXT, but since you didn't declare one, it doesn't do anything; you have to manually release all objects owned by TEXT yourself, in this case the char *text.


Edited by - Stoffel on February 19, 2001 12:28:24 AM
quote:

t = new char[strlen("hello")];



Thank you Stoffel, I made a careless mistake there! Now the delete[] call works fine.

The destructor approach dosn´t work for me. The debugger tells me this: "BLOCK_TYPE_IS_VALID(pHead->Block in use)".

Gandalf the Black


Edited by - Gandalf on February 20, 2001 2:24:46 PM
Gandalf the Black

This topic is closed to new replies.

Advertisement