Not freeing memory in a C array

147 views Asked by At

This C code (compiled as C++) is not freeing memory. The program starts out with 992kB on the 'new' line, then after allocating memory, it goes to 10MB. After freeing the memory, it only goes down to 3MB. Even doing a delete[] doesn't erase the memory. What am I doing wrong?

INT iSize=8192;

struct sUsernameA
{
TCHAR *sUsername;
};

sUsernameA *sArr = new sUsernameA[iSize]();

for (INT i=0;i<iSize;i++)
{
sArr[i].sUsername = (TCHAR*)calloc(512,sizeof(TCHAR));
}


for (INT i=0;i<iSize;i++)
{
free(sArr[i].sUsername);sArr[i].sUsername = NULL;
}

delete [] sArr;
1

There are 1 answers

0
Baltasarq On

There are various approaches to memory management. For example, you can find an explanation to some of them in the wikipedia. I'm used to see dlmalloc around, but obviously there are many other techniques.

In any case, all techniques needs scafdolding, and also some of them try to guess the next moves about memory management by the programmer, so the only way to be sure that you've completely returned all the memory to the OS is to exit your program.