Are delete[] and delete the same?

Started by
8 comments, last by Michael85033 22 years, 4 months ago
This question might sound a little stupid, and all my books tell me that delete[] is for arrays and delete for singular memory blocks. MSDN says that delete[] should be called to free arrays. But if I allocate an array on the heap, fill it with values and then just call delete(array) instead of delete[] array, every element "looks" freed. By that I mean that when I free i.e. an array of int´s, the int´s afterwards become something like -58903490; all of them get the same garbage number. Besides, when I step into the delete[] and delete calls in the debugger, they both seem to call the exact same code. In debug mode, at least, haven´t checked release mode yet. I use MSVC++ 6.0 with Service Pack 5. What´s your opinion on this? Would be cool if I could forget delete[] entirely; Makes smart pointers easier to implement as well.
Advertisement
I''d get an opinion from someone who is a master with the MSVC compiler, but I wouldn''t get into the habit of using delete on arrays.

Maybe MSVC does some checking and calls delete[] for you if you don''t call delete. But, I think using delete[] makes it clear what you''re deleting, sticks to the standard of the C++ language, and would probably make your code more portable should the need arise.

R.
Try using delete on an array of user-defined objects and see how many destructors get called.
The C++ standard says delete [] should be used to deallocate arrays, so even if delete works in MSVC (looks like you are using debug mode), it is not standard, and not portable.
quote:Original post by Anonymous Poster
Try using delete on an array of user-defined objects and see how many destructors get called.

Pay attention to this. delete [] calls destructors, so if you allocate dynamic arrays of objects (not intrinsic types) it ensures that they are properly destroyed and their memory is properly freed.

A good rule of thumb is "If you use [] when allocating (with new), use it when deallocating (delete)."

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
using the delete keyword will only delete the first element of your array, which is also a pointer to your array so you are leaving the rest of your array orphaned in memory.

Since the first element is being free''d this is probably why you think your array is being deleted. I doubt that MSVC will call delete [] for you
Well, it definitely doesn't work under linux GNU compiler (no surprise). I seg faulted and only got one destructor call.

I think the last AP is right, I think it just deletes your first object in the array (since that's what's being pointed to by your pointer). Here's the simple program I used test:


#include .h>

class Test {
public:

Test();
~Test();

private:

};

Test::Test() {

cout << "Constructor called." << endl;

}

Test::~Test() {

cout << "Deconstructor called. << endl;

}

int main() {

Test * test_array = new Test[5];

delete test_array;
// delete[] test_array;

return 0;
}



Edited by - rube on December 11, 2001 4:58:56 PM

Edited by - rube on December 11, 2001 5:00:37 PM
MSVC never calls delete[] for built in types, whether you try to call it or not. operator delete will be called, which just calls free(), so all elements will be de-allocated. But as this is not guaranteed by the standard you should call delete[].
quote:Well, it definitely doesn''t work under linux GNU compiler (no surprise). I seg faulted and only got one destructor call.


MSVC produces the same behaviour, but only when running a debug build. Otherwise you just get one destructor called, but program execution continues.
I swear that this actually happened and wasn''t some other stupid unrelated coding error..

I had a strange intermittent crashing bug in my program..could never reproduce the bug, but it was just a personal tool I wrote, not a distributable app, so no big deal..I heard that MSVC++ automatically replaced the delete with delete[] where appropriate, so I didn''t worry about..

Later I ported to gcc, and had to manually rename all my delete''s to delete[]''s..

Guess what?..the intermittent bug in the MSVC++ version went away..the gcc version never got it..

Now I use always use delete[] for arrays, it''s become a habit..






"Like all good things, it starts with a monkey.."
"Like all good things, it starts with a monkey.."

This topic is closed to new replies.

Advertisement