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

    (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


    or do i need to free it like this:


    consider this:
    p=func2(...) /* p is an integer pointer */
    }/* end func1 */

    int *func2(...)
    int *P;
    p= malloc(n*sizeof 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.

    Hassan Iqbal, Sep 25, 2003
    1. Advertisements

  2. This question comes up fairly regularly here.

    No it isn't.

    Yes you do.
    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+++|
    | W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "Immanuel Kant but Genghis Khan."
    - The Official Graffitist's Handbook
    Joona I Palaste, Sep 25, 2003
    1. Advertisements

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

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

    That's what you need to free it all, indeed.
    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, Sep 25, 2003
  4. Hassan Iqbal

    Al Bowers Guest

    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
    p = malloc(n*sizeof *p); /* or n*sizeof(int *) */

    p = malloc(n * sizeof **p); /* or n*sizeof(int) */
    This is wrong.

    This is correct way to free the allocations.
    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, Sep 25, 2003
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.