#### Archived

This topic is now archived and is closed to further replies.

# About the STL function "erase"

This topic is 6454 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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 text; }

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

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

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

  std::vector vector1;std::vector 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 items;std::vector 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

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites
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

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 11
• 14
• 9
• 10
• 12
• ### Forum Statistics

• Total Topics
633003
• Total Posts
3009830
• ### Who's Online (See full list)

There are no registered users currently online

×