G
Guru
Hi ,
I have observed an in-consistent behavior in malloc/free .
Case 1 :
struct TMemFile
{
char* pFIle;
}
main1()
{
TMemFile mptr[20000] ; // Stack Variable
for ( 20000 times)
{
mptr.pFile = malloc 100 KB.
}
for (20000 times)
{
Free(mptr.pFile);
}
}
Case 2:
struct TMemFile
{
char* pFIle;
}
main2()
{
TMemFile* mptr[20000] ; // Heap Variable
for (20000 times)
{
mptr = malloc (TMemFile);
mptr->pFile = malloc 100 KB;
}
for (20000 times)
{
free(mptr->pFIle);
free(mptr);
mptr = NULL;
}
}
In Case 1, after executing main1() memory is getting released and
VirtualMemory is coming down (reading from top).
In Case 2, after executing main2() memory is getting released and
VirtualMemory is not coming down (reading from top).
1) Why exceptional behavior for pointer variable (case 2
TMemFile* ) ?
2) If we consider case 2 then what are the different consequence for
below two scenerio,
i) Normal executable.
ii) Dynamically loaded libs (open by dlopen())
I have observed an in-consistent behavior in malloc/free .
Case 1 :
struct TMemFile
{
char* pFIle;
}
main1()
{
TMemFile mptr[20000] ; // Stack Variable
for ( 20000 times)
{
mptr.pFile = malloc 100 KB.
}
for (20000 times)
{
Free(mptr.pFile);
}
}
Case 2:
struct TMemFile
{
char* pFIle;
}
main2()
{
TMemFile* mptr[20000] ; // Heap Variable
for (20000 times)
{
mptr = malloc (TMemFile);
mptr->pFile = malloc 100 KB;
}
for (20000 times)
{
free(mptr->pFIle);
free(mptr);
mptr = NULL;
}
}
In Case 1, after executing main1() memory is getting released and
VirtualMemory is coming down (reading from top).
In Case 2, after executing main2() memory is getting released and
VirtualMemory is not coming down (reading from top).
1) Why exceptional behavior for pointer variable (case 2
TMemFile* ) ?
2) If we consider case 2 then what are the different consequence for
below two scenerio,
i) Normal executable.
ii) Dynamically loaded libs (open by dlopen())