In-consistent behavior in malloc/free .

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())
 
J

John H.

            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).


I don't see any problem with the logic of you pseudo-code, but I might
not be reading it correctly. We might be able to help you more if you
posted actual C++ code.
Also, once you start talking about Virtual Memory, you are going off
the topic of this Usenet group. If I recall correctly, the C++
language does not describe Virtual Memory at all. It is platform
specific behavior, most likely associated with your operating system
(e.g. Microsoft Windows XP). As such, you might get better help from
a platform specific group (e.g. comp.os.ms-windows.programmer.memory
or microsoft.public.vc.language).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top