Note that there are some explanatory texts on larger screens.

plurals
  1. POIs delete p where p is a pointer to array always a memory leak?
    primarykey
    data
    text
    <p>following a discussion in a software meeting I've set out to find out if deleting a dynamically allocated, primitives array with plain <code>delete</code> will cause a memory leak. </p> <p>I have written this tiny program and compiled it with visual studio 2008 running on windows XP:</p> <pre><code>#include "stdafx.h" #include "Windows.h" const unsigned long BLOCK_SIZE = 1024*100000; int _tmain() { for (unsigned int i =0; i &lt; 1024*1000; i++) { int* p = new int[1024*100000]; for (int j =0;j&lt;BLOCK_SIZE;j++) p[j]= j % 2; Sleep(1000); delete p; } } </code></pre> <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 </p> <p>I've modified my test program to allocate a non primitive type array :</p> <pre><code>#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 &lt; 1024*100000; i++) { aStruct* p = new aStruct[1024*100000]; Sleep(1000); delete p; } } </code></pre> <p>after running for for 10 minutes there was no meaningful increase in memory </p> <p>I compiled the project with warning level 4 and got no warnings.</p> <p>is it possible that the visual studio run time keep track of the allocated objects types so there is no different between <code>delete</code> and <code>delete[]</code> in that environment ? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload