Advertisement

How should I delete a char*

Started by August 11, 2000 04:54 AM
8 comments, last by Esap1 24 years, 4 months ago
How would I delete this. char* string; string = new char[23]; ??? How would I delete it. I originally thought it would be this: delete string; then I tried this: delete [] string; I am getting an ASSERT Error. I thought you get this when your program trys to delete something that doesnt exist. Please help, its late, and Im very tired, so Ill see yal in mornin,fdsfsfsafsfa
The last one "delete[] string" is correct. I think that you may write ouside the array bounds. Something like string[30]=''a'';
This oculd cause the assertion.
Yoshi
The last truth is that there is no magic(Feist)
Advertisement
If you allocate an array (new foo[]), then you need to delete an array (delete [] pFoo).

The usual difference is that the normal delete (without the []) doesn''t go through and call the destructors for all of the objects in the array so you may get leaks. "But char doesn''t have a destructor!" you''re saying. Makes no difference, don''t get into the bad habit.

If you''re doing what you think is the right thing and getting asserts try it on a two-line program and see if it repro''s. If not then you may be overwriting your memory as Yoshi suggested. If it does repro then maybe post your test case and mention the name and version of the compiler, options used when building, etc.

-Mike
I am doing this:
    Map_loaded=NULLMap_loaded = new char[strlen(string)];strcpy(Map_loaded, string);//At the End of Programif(Map_loaded!=NULL){  //HERE IS WHERE THE ASSERT ERROR IS!!!  delete [] Map_loaded;}    

Whats the matter?
It looks like you are writing beyond the bounds of the array:

I assume ''string'' is a NULL terminated string (since you use strcpy()) on it. However, strlen() does not count the NULL character, so in order to allocate enough space, you need to allocate strlen(string) + 1 characters.
Thanks a lot, I dont use strlen() much and didnt know that it doesnt count the NULL char at the end, thanks. I realize that strcpy() didnt have enough room to copy the NULL char at the end Though I dont understand why it gave me an error when I tried to delete it, was it because delete didnt know how long it was?
Thanks A LOT

PS: You could help this board a lot, why dont u register a name
Advertisement
Anyone know?
When you allocate memory in debug mode, VC++ doesn''t just allocate the memory itslef,but some bytes in front and at the end of the allocated memory. When you free the memory VC++ will check these bytes to see if anything went wrong with the memory. And if there are no check bytes anymore (you have deleted them by writing data over them) VC++ throws an assertion. Look for the details in the VisualC++ help file.

Yoshi
The last truth is that there is no magic(Feist)
Thanks a lot, I was wondering how they did that
Yep, this is one of the many "careful" additions MSVC adds in debug mode. I think that in the release mode it would give you a fatal exception on the strcpy in runtime.

This topic is closed to new replies.

Advertisement