In-consistent behavior in malloc/free .

Discussion in 'C++' started by Guru, Apr 28, 2010.

  1. Guru

    Guru Guest

    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())
     
    Guru, Apr 28, 2010
    #1
    1. Advertisements

  2. Guru

    John H. Guest

    On Apr 28, 5:48 am, Guru <> 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.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).
     
    John H., Apr 28, 2010
    #2
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    13
    Views:
    923
  2. Peter
    Replies:
    34
    Views:
    2,583
    Richard Tobin
    Oct 22, 2004
  3. kj
    Replies:
    4
    Views:
    1,008
    Jonathan Bartlett
    Dec 6, 2004
  4. porting non-malloc code to malloc

    , Feb 18, 2005, in forum: C Programming
    Replies:
    3
    Views:
    697
    Walter Roberson
    Feb 19, 2005
  5. Rajshekhar
    Replies:
    5
    Views:
    3,474
    Jonathan Bartlett
    Mar 29, 2005
  6. Johs32

    to malloc or not to malloc??

    Johs32, Mar 30, 2006, in forum: C Programming
    Replies:
    4
    Views:
    556
    Captain Winston
    Mar 30, 2006
  7. george
    Replies:
    0
    Views:
    1,414
    george
    Aug 29, 2008
  8. mohammed_a_o
    Replies:
    0
    Views:
    551
    mohammed_a_o
    Nov 30, 2010
Loading...