Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is delete p where p is a pointer to array always a memory leak?

following a discussion in a software meeting I've set out to find out if deleting a dynamically allocated, primitives array with plain delete will cause a memory leak.

I have written this tiny program and compiled it with visual studio 2008 running on windows XP:

#include "stdafx.h"
#include "Windows.h"

const unsigned long BLOCK_SIZE = 1024*100000;
int _tmain()
{
    for (unsigned int i =0; i < 1024*1000; i++)
    {
        int* p = new  int[1024*100000];
        for (int j =0;j<BLOCK_SIZE;j++) p[j]= j % 2;
        Sleep(1000);
        delete p;
    }
}

I than monitored the memory consumption of my application using task manager, surprisingly the memory was allocated and freed correctly, allocated memory did not steadily increase as was expected

I've modified my test program to allocate a non primitive type array :

#include "stdafx.h"
#include "Windows.h"


struct aStruct
{
    aStruct() : i(1), j(0) {}

    int i;
    char j;
} NonePrimitive;

const unsigned long BLOCK_SIZE = 1024*100000;
int _tmain()
{
    for (unsigned int i =0; i < 1024*100000; i++)
    {
        aStruct* p = new  aStruct[1024*100000];
        Sleep(1000);
        delete p;

    }
}

after running for for 10 minutes there was no meaningful increase in memory

I compiled the project with warning level 4 and got no warnings.

is it possible that the visual studio run time keep track of the allocated objects types so there is no different between delete and delete[] in that environment ?

like image 838
Eli Avatar asked Mar 09 '10 09:03

Eli


People also ask

How are memory leaks caused in c++?

Memory leakage occurs in C++ when programmers allocates memory by using new keyword and forgets to deallocate the memory by using delete() function or delete[] operator. One of the most memory leakage occurs in C++ by using wrong delete operator.

How do you prevent memory leaks?

To avoid memory leaks, memory allocated on heap should always be freed when no longer needed.

Can we use delete with malloc?

You can use malloc() and new in the same program. But you cannot allocate an object with malloc() and free it using delete . Nor can you allocate with new and delete with free() or use realloc() on an array allocated by new .


1 Answers

delete p, where p is an array is called undefined behaviour.

Specifically, when you allocate an array of raw data types (ints), the compiler doesnt have a lot of work to do, so it turns it into a simple malloc(), so delete p will probably work.

delete p is going to fail, typically, when:

  • p was a complex data type - delete p; won't know to call individual destructors.
  • a "user" overloads operator new[] and delete[] to use a different heap to the regular heap.
  • the debug runtime overloads operator new[] and delete[] to add extra tracking information for the array.
  • the compiler decides it needs to store extra RTTI information along with the object, which delete p; won't understand, but delete []p; will.
like image 110
Chris Becke Avatar answered Sep 28 '22 12:09

Chris Becke