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

Posted by Eli on Stack Overflow See other posts from Stack Overflow or by Eli
Published on 2010-03-09T09:01:51Z Indexed on 2010/03/09 9:06 UTC
Read the original article Hit count: 438

Filed under:
|

following a discussion in a software meeting I setup to find out if deleting an dynamically allocated primitive array with plain delete will cause a memory leak.

I have written this tiny program and compiled 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 ?

© Stack Overflow or respective owner

Related posts about c++

Related posts about memory-management