Reputation: 1236
When I use new []
to apply memory. In the end, I use delete
to free memory (not delete[]
). Will this cause memory leak?
Two types:
int
, char
, double
...I think may free leak. because the destruct function.
Many people and some books tell me, new[]
-> delete[]
; new
-> delete
.
I want to know why.
so I check vs2010 source code, it must be use memory pool mechanism. It is long and complex . I can't continue read.
How are delete
and delete[]
implemented?
Upvotes: 2
Views: 3627
Reputation: 473272
It's not a memory leak; it's undefined behavior. That's much worse.
Calling delete
on memory allocated by new[]
can lead to corrupting the heap, which can crash your application. Not immediately, of course, because that would be too useful. It will crash sometime later, long after the heap was corrupted to make it almost impossible to track down.
Always use delete[]
with new[]
. Actually, never use new[]
to begin with. Use a std::vector
unless you have very special and specific needs.
I want to know why ?
Why does it matter? It's wrong and leads to a broken program. It's wrong because the C++ standard says so.
However, if you insist on a reason...
Notice that you can allocate any number of items with new[]
. You can put any counting number (aka: positive integer) there. So you can have new int[50]
or new int[someIntegerVariable]
. It's all good.
Also notice that delete[]
does not take a count. Well... how is that possible? If you allocate 50 int
s, obviously delete
needs to know how many you allocated, right? But you don't have to tell it; it automagically knows.
How? Because new int[50]
allocates more than just an array of 50 int
s. It also allocates enough space to hold the size of the allocation. Think of it as allocating 51 int
s but only letting you use 50 of them.
Then, delete[]
comes along. It figures out where to find the count that new[]
allocated. And it uses that count to deallocate the memory.
delete
does not do this. Therefore, it cannot deallocate the memory correctly.
Upvotes: 5
Reputation: 361332
When I use new [] to apply memory. In end , I use delete to free memeory(not delete[]).Must be memory leak ?
If you use new[]
, and then delete
(not delete[]
), then according to the langauge specification, it invokes undefined-behavior which means anything could happen. It may cause memory leak or may not. No such guarantee is given by the language, nor by the compiler.
Many people and some books tell me, new[]->delete[]; new->delete . I want to know why ?
Short answer : because the specification says so.
Long answer: the functions which implementnew
and new[]
are implemented differently: new
assumes memory of one item is to be allocated, while new[]
assumes memory of n
item is to be allocated where n
is passed to the function as argument. So to undo the process (i.e to deallocate the memory), there should be two functions as well : delete
and delete[]
which are implemented differently, each makes some assumption about the memory allocated by their counterparts. For example, delete
simply assumes memory allocated for one item is to be deallocated; on the other hand delete[]
needs to know the number of items for which memory is to be deallocated, so it assumes that new[]
stores the number of items somewhere in the allocated memory, most commonly in the begginning of the allocated memory, and so delete[]
first reads that portion of memory which stores the number of items, and then deallocates the memory, accordingly.
Note that new
and new[]
each calls the default constructor to construct the object(s) in the allocated memory, and delete
and delete[]
calls the destructor to destruct the object before deallocating the memory.
Upvotes: 4
Reputation: 4407
Nawaz is correct that it is 'because the specification says so'.
The implementation reason behind it is that when you call delete
, the compiler adds a call to the destructor of the object before releasing the memory.
In the case of an array, the number of calls to destructors depends on the number of objects allocated which is only known at runtime. So new[]
saves extra information regarding number of objects which delete[]
uses.
This information is not needed when using new
/delete
.
Upvotes: 1
Reputation: 2691
You should not do so. Though, for most implementations, you can safely delete a array with delete instead of delete[], as long as the element type of the array don't have destructor.
For a type with destructor, new [] will have to record the length of the array somewhere in the dynamic allocated memory block. delete[] will destruct every element base on that, and finally free the memory block. And delete will not recognize such a structure. Maybe it's memory leak, maybe you will get some exception.
Upvotes: 0