Delete [] is killing me!! =(

Started by
27 comments, last by Red Ant 22 years, 6 months ago
Does the trivial int main() example cause the failure, other than
"return 1;" returning an error? My guess would be that you have this delete in a distructor, and you are using the default copy constructor. Somewhere you are creating a temporary, and that's what is causing the double deletion. Put a cerr << "Delete " << p << endl; before the delete [], Or set a breakpoint, depending on your style. You can also always set pointers to null after you delete them, this tends to make things blow up at a more instructive time. The only problem with setting the pointer to NULL is that if the only problem is the multiple delete's, this fixes the problem as delete [] 0; is not an error, though it may mean you were running around with a dangerous NULL longer than good practice really would allow.

Edited by - grib on October 12, 2001 10:55:43 PM
Advertisement
Hmm, it seems I wasn''t telling the truth in all points, folks. I just tried that little snippet from my first post again, and this times it worked. Hmm, funny ain''t it? Anyhow, Grib you are right, I do have this delete [] in the destructor of my Text class (where it is supposed to free up the memory allocated for a string). In what way could this cause a problem? And what do you mean by "Somewhere you are creating a temporary..." . Could you elaborate a bit? Thanks.
For one thing, your compiler already knows the size of a char, so it is not wise to write

new char[sizeof (char) * 100]

What this will do is give you an array of 800 elements.

The correct way is to declare it as

char* ptr = new char[100];

Now you have a pointer to an array of 100 chars. Like I said before, the compiler will already know how big to make these "elements". For example, if you use pointer arithmetic like so (ptr++), you will actually be advancing 8 bytes each time, not just one, because the compiler will see that the pointer is pointing to byte sized data types. A lot of people trip up on this.

Anyway, I tried your code in my MSVC++ 6.0 and it did not crash for me.

-nt20
Actually, I thought the standard said that sizeof(char) must evaluate to 1...

"Don''t be afraid to dream, for out of such fragile things come miracles."
Yes, hadn''t thought of that - it does evaluate to one.

Which means that you don''t need it in the new declaration anyway then (bloated code! )

Plus it''s not a good thing to do, since when you start using arrays of objects or arrays of pointers, you don''t want to declare them with the sizeof() function. The compiler will figure out the size for you, that was my point. It is dangerous, that''s all.

-nt20
hi peeps. This is a good point to bring up, cause i get the exact same error with my dynamic allocation code... Strange isnt it? The program compiles and runs perfectly,until i exit out at which time I get the following dialogue thrown at me:



I've been up and down these boards, but cant straighten this out. Im really about to go back to hard coding all my arrays...

Edited by - shdaga on October 12, 2001 12:55:54 AM

Edited by - shdaga on October 12, 2001 12:58:03 AM

Edited by - shdaga on October 13, 2001 8:28:16 AM

Edited by - shdaga on October 13, 2001 8:33:52 AM
Here's a new dance everyone can do, do the Kirby:<(*.*<) <(^.^)> (>*.*)> <(^.^)> <(*.*<) (>^.^)>
shdaga, we can''t see the pic cuz it''s stored locally on your hard disc. You need to upload it to a server and then post the URL. Is it the same error message I''m getting, by the way?
That was me talking, by the way. =)
well, i tried uploading my pics to my website space at angelfire, but I dont think that did the trick.. Anyway, The picture is an error dialogue box with the exact same error you got. Creepy? I think so. Annoying? Absolutely.
Here's a new dance everyone can do, do the Kirby:<(*.*<) <(^.^)> (>*.*)> <(^.^)> <(*.*<) (>^.^)>
int main(){
char *ptr;
ptr = new char[sizeof(char) * 100];
delete [] ptr;
return 1;
}

IT IS CRASHING BECAUSE YOU ARE DELETING AN ARRAY OF CHAR THAT HAS NO ''\0'' CHARACTER IN THE FINAL POSITION.
YOU ARE USING DELETE [] PROPERLY, BUT THE ERROR RAISES BECAUSE OF ''\0'' IS NOT THERE TO INDICATE THAT ARRAY OF CHAR HAS AN END

HOPE I CAN HELP U

BYE
GOOD LUCK
MARCOS


game developer

This topic is closed to new replies.

Advertisement