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

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

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:
    734
  2. ravi
    Replies:
    0
    Views:
    481
  3. Peter
    Replies:
    34
    Views:
    2,055
    Richard Tobin
    Oct 22, 2004
  4. kj
    Replies:
    4
    Views:
    773
    Jonathan Bartlett
    Dec 6, 2004
  5. Rajshekhar
    Replies:
    5
    Views:
    2,342
    Jonathan Bartlett
    Mar 29, 2005
Loading...

Share This Page