some queries on freeing memory allocated using malloc

Discussion in 'C Programming' started by Hassan Iqbal, Sep 25, 2003.

  1. Hassan Iqbal

    Hassan Iqbal Guest

    Hi,
    (1)if i do the memory allocation like:
    int **p;
    p= malloc(n*sizeof p);
    for(i=0;i<n;i++) /* i and n are integers */
    p= malloc(n*sizeof p);

    then in that case is this command sufficient to free all the allocated
    memory:

    free(p);

    or do i need to free it like this:

    for(i=0;i<n;i++)
    free(p);
    free(p);

    (2)
    consider this:
    func1(...)
    {
    ...
    ...
    for(i=0;i<n;i++){
    p=func2(...) /* p is an integer pointer */
    ...
    ...
    }
    ...
    }/* end func1 */

    int *func2(...)
    {
    int *P;
    p= malloc(n*sizeof p);
    ...
    ...
    return(p);
    }/* end func2 */

    in the above pseudo code where should i free the memory which has been
    allocated in func2, supposing that n is significantly large. should it
    be every time just before the call to func2 or at the end of func1 or
    after return(p) in func2 itself.

    thanks,
    hassan
     
    Hassan Iqbal, Sep 25, 2003
    #1
    1. Advertising

  2. Hassan Iqbal <> scribbled the following:
    > Hi,


    This question comes up fairly regularly here.

    > (1)if i do the memory allocation like:
    > int **p;
    > p= malloc(n*sizeof p);
    > for(i=0;i<n;i++) /* i and n are integers */
    > p= malloc(n*sizeof p);


    > then in that case is this command sufficient to free all the allocated
    > memory:


    > free(p);


    No it isn't.

    > or do i need to free it like this:
    >
    > for(i=0;i<n;i++)
    > free(p);
    > free(p);


    Yes you do.

    > (2)
    > consider this:
    > func1(...)
    > {
    > ...
    > ...
    > for(i=0;i<n;i++){
    > p=func2(...) /* p is an integer pointer */
    > ...
    > ...
    > }
    > ...
    > }/* end func1 */


    > int *func2(...)
    > {
    > int *P;
    > p= malloc(n*sizeof p);
    > ...
    > ...
    > return(p);
    > }/* end func2 */


    > in the above pseudo code where should i free the memory which has been
    > allocated in func2, supposing that n is significantly large. should it
    > be every time just before the call to func2 or at the end of func1 or
    > after return(p) in func2 itself.


    It should be in the code in func1(). If you add the free(p) code in
    func2() after the return(p), it will never get executed, resulting in
    memory leaks. Code after a return statement is *never* executed, no
    matter what code it is, or which function it appears in.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "Immanuel Kant but Genghis Khan."
    - The Official Graffitist's Handbook
     
    Joona I Palaste, Sep 25, 2003
    #2
    1. Advertising

  3. Hassan Iqbal wrote:

    > (1)if i do the memory allocation like:
    > int **p;
    > p= malloc(n*sizeof p);


    We'll give you the benefit of the doubt (for code written instead of
    copy-pasted), but the statement above will not exactly do what you expect.

    p = malloc(n * sizeof *p);

    > for(i=0;i<n;i++) /* i and n are integers */
    > p= malloc(n*sizeof p);


    Same here: p = malloc(n * sizeof **p);

    >
    > for(i=0;i<n;i++)
    > free(p);
    > free(p);
    >

    That's what you need to free it all, indeed.

    > in the above <snipped> pseudo code where should i free the memory which has been
    > allocated in func2, supposing that n is significantly large. should it
    > be every time just before the call to func2 or at the end of func1 or
    > after return(p) in func2 itself.
    >

    You are quite free to free p wherever between the place it is allocated
    at, and the place you last use it. The extreme last place where you can
    free p is when it goes out of scope, likely when you are going to leave
    the block (between the { and the } ) where p has been declared.

    --
    Bertrand Mollinier Toublet
    Blog: http://www.bmt.dnsalias.org/blog
     
    Bertrand Mollinier Toublet, Sep 25, 2003
    #3
  4. Hassan Iqbal

    Al Bowers Guest

    Hassan Iqbal wrote:
    > Hi,
    > (1)if i do the memory allocation like:


    You are allocating a multidimensional array of type int. You
    need to get the sizes correct. Look at the faq question 6.16
    located at http://www.eskimo.com/~scs/C-faq/q6.16.html

    > int **p;
    > p= malloc(n*sizeof p);


    p = malloc(n*sizeof *p); /* or n*sizeof(int *) */

    > for(i=0;i<n;i++) /* i and n are integers */
    > p= malloc(n*sizeof p);


    p = malloc(n * sizeof **p); /* or n*sizeof(int) */

    >
    > then in that case is this command sufficient to free all the allocated
    > memory:
    >
    > free(p);


    This is wrong.

    >
    > or do i need to free it like this:
    >
    > for(i=0;i<n;i++)
    > free(p);
    > free(p);
    >

    This is correct way to free the allocations.

    > (2)
    > consider this:
    > func1(...)
    > {
    > ...
    > ...
    > for(i=0;i<n;i++){
    > p=func2(...) /* p is an integer pointer */
    > ...
    > ...
    > }
    > ...
    > }/* end func1 */
    >
    > int *func2(...)
    > {
    > int *P;
    > p= malloc(n*sizeof p);
    > ...
    > ...
    > return(p);
    > }/* end func2 */
    >
    > in the above pseudo code where should i free the memory which has been
    > allocated in func2, supposing that n is significantly large. should it
    > be every time just before the call to func2 or at the end of func1 or
    > after return(p) in func2 itself.
    >


    You are allocating the space in func2 which is returning a pointer
    to the allocated space. You will then use this space in func1 for
    some purpose. Once you are finished using the space then
    deallocate (free) it. Beware for the hazards in func2 should there
    be a allocation failure. In the case you will need to recover from
    the allocation failure and signal func2 of the failure. This is
    usually done by func2 returning NULL.

    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x)
    http://www.geocities.com/abowers822/
     
    Al Bowers, Sep 25, 2003
    #4
    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. Replies:
    5
    Views:
    662
    Matt Wharton
    Dec 9, 2004
  2. binaya

    freeing allocated memory

    binaya, Oct 19, 2003, in forum: C Programming
    Replies:
    11
    Views:
    615
    James Hu
    Oct 19, 2003
  3. Curley Q.

    freeing allocated memory

    Curley Q., Apr 28, 2004, in forum: C Programming
    Replies:
    7
    Views:
    394
    Sam Dennis
    Apr 30, 2004
  4. Peter
    Replies:
    34
    Views:
    2,049
    Richard Tobin
    Oct 22, 2004
  5. Gene
    Replies:
    0
    Views:
    465
Loading...

Share This Page