#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    2
    Rep Power
    0

    Memory allocation inside of a function that returns double pointer


    I would like to know if there is any problem with
    not freeing the memory I allocate inside a function that returns a double pointer
    Example:

    void main()
    {

    int **transpose(int **a,int n,int m);

    int **b;

    mat=imatrix(0,3,0,4); /*this calls a function that allocates memory for the array of integers */

    mat[0][0] = 0;
    mat[0][1] = 0;
    mat[0][2] = 0;
    mat[0][3] = 0;
    mat[1][0] = 1;
    mat[1][1] = 2;
    mat[1][2] = 3;
    mat[1][3] = 4;
    mat[2][0] = 5;
    mat[2][1] = 6;
    mat[2][2] = 7;
    mat[2][3] = 8;

    b=imatrix(0,4,0,3);
    b=transpose(mat,3,4);


    free_imatrix(b,0,4,0,3); /* free the allocated memory inside the main() */
    }

    int **transpose(int **a,int n,int m) {
    /*function that takes a[0,..,n][0,...,m] as input and gives its transpose as output */
    int i,j;
    int **b;

    b=imatrix(0,m,0,n); /*allocate memory for the array of integers with indices [0,..,m][0,...n]*/

    for (i=0;i<n;i++)
    for (j=0;j<m;j++)
    b[j][i]=a[i][j];
    return b;

    }



    The question is. What happens if I do not free the memory allocated for variable b inside the function "transpose".

    It runs free of errors like this but I am concerned when I have to use it many times andf with large matrices.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    > void main()
    main returns an int

    > What happens if I do not free the memory allocated for variable b inside the function "transpose".
    You don't free the matrix created within imatrix either.
    Actually, nothing happens, so long as you keep a pointer to the memory alive.

    Given the code you've posted, you would finish with say
    Code:
    free_imatrix(b,0,4,0,3); /* free the allocated memory inside the main() */
    free_imatrix(mat,0,3,0,4);
    So long as your imatrix() and free_imatrix() calls match up, I don't see any problems with memory leaks here.
  4. #3
  5. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,805
    Rep Power
    1570
    On the one hand, it isn't necessary to free memory allocated in a given function, provided that the memory is freed somewhere in the program run.

    OTOH, it is better to localize the allocation and deallocation of memory as much as possible, to limit the chances of a memory leak. If the memory is used only within the function, then you should free it within that function; similarly, if you are replacing a pointed-to block with a newly allocated one, then you should make sure that all pointers to the original are updated, then free the original block once you have finished with it. Conversely, you don't want to free memory that might be aliased to more than one pointer.

    In the case in question, the problem is that you are assigning a newly-allocated value to the variable b in main() (poor naming, BTW - having two variables with the same name in overlapping scopes can be confusing), then immediately overwriting the pointer without freeing the memory it points to. The line

    Code:
    b=imatrix(0,4,0,3);
    is not merely unnecessary, it introduces a memory leak that is completely avoidable.

    Also, as Salem points out, you will want to deallocate both of the final values at the end of the program. While most systems will automatically free all allocated memory at the close of a program, relying on this is extremely poor practice; never leave allocated memory unfreed at the end of the program.
    Last edited by Schol-R-LEA; March 17th, 2006 at 08:37 PM.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    2
    Rep Power
    0
    I would like to thank you both for taking the time.
    What you said it was more than enough to answer my question.

    Codrut

IMN logo majestic logo threadwatch logo seochat tools logo