Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > In-consistent behavior in malloc/free .

Reply
Thread Tools

In-consistent behavior in malloc/free .

 
 
Guru
Guest
Posts: n/a
 
      04-28-2010
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[i].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[i] = malloc (TMemFile);
mptr[i]->pFile = malloc 100 KB;
}
for (20000 times)
{
free(mptr[i]->pFIle);
free(mptr[i]);
mptr[i] = 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())



 
Reply With Quote
 
 
 
 
John H.
Guest
Posts: n/a
 
      04-28-2010
On Apr 28, 5:48*am, Guru <(E-Mail Removed)> wrote:
> * * * * * * 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[i].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[i] = malloc (TMemFile);
> * * * * * * * * * * * * mptr[i]->pFile = malloc 100 KB;
> * * * * * * }
> * * * * * * for (20000 times)
> * * * * * * {
> * * * * * * * * * * * * free(mptr[i]->pFIle);
> * * * * * * * * * * * * free(mptr[i]);
> * * * * * * * * * * * * mptr[i] = 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).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VERY odd routing behavior when attempting VPN connections over Wifi Robert Gordon Wireless Networking 0 08-25-2005 04:04 PM
Question about behavior of View Available Networks window mszablocky Wireless Networking 4 07-24-2005 08:28 PM
Firefox under Linux -- odd behavior Dennis J. Tuchler Firefox 0 07-28-2004 04:05 PM
[FF] middle button behavior Gillou Firefox 0 05-01-2004 08:46 AM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments