Saeed Masoomi
Saeed Masoomi

Reputation: 1834

deleting c++ array from heap and memory leak

I have a question regarding deleting an array from heap memory. In a book and on this blog and in other resources such as this one, I read that for removing an array from the heap we must use the [] after the delete keyword so that if we do not use [] we will have leak memory.

for example, consider the code below.

//constructing array
int *s = new int[10];


// deleting array from heap
delete [] s;

I tested this little program in Linux by using the valgrind package to check how much memory leaks we have which are caused by bad coding. By below command in Linux, I saw that everything is alright

sudo valgrind --leak-check=full ./<path_to_exe_file>

this is the output of the Linux command

 ==4565== HEAP SUMMARY:
 ==4565==     in use at exit: 0 bytes in 0 blocks
 ==4565==   total heap usage: 1 allocs, 1 frees, 40 bytes allocated
 ==4565== 
 ==4565== All heap blocks were freed -- no leaks are possible

However, My question arose when I tried to use the delete without using []. The output from valgrind shows that all heap memory has been freed. Is this correct? or valgrind didn't realize the heap wasn't freed and some part of the array is still in there!!? If valgrind cannot detect this kind of memory leak, is there any other package that can detect this?

Upvotes: 12

Views: 9466

Answers (2)

Martin Broadhurst has already given the correct language-lawyer answer. I'm going to give the technical detail answer:

The point about using delete[] over delete is, that there is no way for the delete operator to know whether the passed pointer points to an array or to a single object. As such, delete only deletes a single object, while delete[] invokes some additional magic to recover the size of the array, and proceeds to delete all the elements.

Now deleting consists of two distinct parts:

  1. The objects must be destroyed by calling destructors. For an array, this means one destructor call for each array element.

  2. The memory that was used must be marked as free so that it may be reused. This is the job of the global operator delete() in C++. Since arrays are stored consecutively, this is a single call for the entire array.

valgrind is only concerned about memory. As such, it hooks memory allocating functions like malloc(), free(), operator new(), and operator delete().

What happens when you call delete instead of delete[] is, that the first object is destructed, and the pointer is passed on to operator delete(). operator delete() does not know about the object(s) that were stored inside the memory region, they are destroyed already anyway, so it will successfully mark the memory region as free. valgrind sees this operator delete() call, and is happy since all memory is free for reuse. However, your code failed to destruct all but the first array elements properly. And this is bad.

Upvotes: 5

user325117
user325117

Reputation:

Calling delete on an array without using [] results in Undefined Behaviour. The Undefined Behaviour might be that the array is correctly deleted, which appears to be what you observed. You can't rely on this, however.

Upvotes: 7

Related Questions