Delete [] is killing me!! =(

Started by
27 comments, last by Red Ant 22 years, 7 months ago
Hi peeps, I've been having the problem I'm about to describe for more than half a year now, and I still don't know what might be causing it. Hopefully you guys can help me out. Okay, here goes. Using
delete []  
in my code makes my programs crash every time. It compiles just fine without any errors or warnigns but when I execute it, I always get a message like this: Debug Assertion failed Program: C:\VISUAL C++\PROJECTS\DATORGT5\DATORGT5.EXE File: dbgdel.cpp Line: 47 Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) Now, I know how to use delete and I don't think I'm doing anything wrong, so why doesn't
delete []  
work for me?? Even something like this:

int main()
{
    char *ptr;

    ptr = new char[sizeof(char) * 100];
    delete [] ptr;
    return 1;
}
  
will lead to a crash 100% of the time. :confused: Please help me if you can. Thanks in advance. Edited by - Red Ant on October 12, 2001 10:09:31 PM
Advertisement
just delete ptr. You only use the [] when its an array you want to delete. Also make sure the pointer isn''t null when you delete it.

Mike Barela
MikeB@yaya.com
Mike BarelaMikeB@yaya.com
He is deleting an array. "delete []" and "delete" are normally the same for types the same for built in types that have no destructors, but it''s best to use the correct one anyway. BTW: Use "new char[100]", not "new char[sizeof(char) * 100]". No, that''s probably not what''s causing your problem. Assuming you have MSVC, try it in release mode instead of debug...

[Resist Windows XP''s Invasive Production Activation Technology!]
Hi mbarela,

Thanks for quick reply, but aren''t I trying to delete an array?? After all I allocated 100 bytes of memory in my example, which I''m trying to free up using delete.
quote:Original post by mbarela
just delete ptr. You only use the [] when its an array you want to delete. Also make sure the pointer isn''t null when you delete it.



Well, he''s actually trying to delete an array of characters so the use of [] is appropiate in the snippet. A general rule is that if you new''ed it with [] then use [] when you delete.

You are also allowed to delete pointers that are NULL. But it is a good idea to check .

As to the original question. There is no reason for that snippet to fail. I suspect that there may be problems elsewhere in your code such as trying to access the pointer after it has been deleted or is set to NULL.

-------
Andrew

Null And Void, I tried compiling in release mode but I get the same error. Thanks anyway.
acraig, I really don''t think the problem lies elsewhere in my code. If I comment all deletes out, then my program works just fine. But as soon as I put the deletes back in it crashes again. I''m simply just puzzled.
I was thinking that something along the lines of:

    char *ptr = NULL;  ptr = new char[sizeof(char) * 100];  char* newPointer = ptr;  delete []ptr;  newPointer[0] = ''A''; //Invalid access.   


This will cause a crash if the delete is left in.


The best thing to do is to use your debugger and step through the code to see where the crash occurs exactly and under what circumstances.



-------
Andrew

if you''ve stepped through your code and you''re convinced nothing is working, i''d suggest uninstalling and reinstalling visual c++. how much ram do you have?

a2k
------------------General Equation, this is Private Function reporting for duty, sir!a2k
Oh, boy I''d rather not do that. I prefer to go over it again. Maybe you are right and there really is a bug somewhere in my code. I''ve got 256 MB of RAM, by the way.

This topic is closed to new replies.

Advertisement