Freeing Dynamic allocated memory

This is a discussion on Freeing Dynamic allocated memory within the C Programming forums, part of the General Programming Boards category; ====== SOLVED ====== Hi errbody! Doing this school assignment, and i need some advice/help with freeing an array of dynamic ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    5

    Freeing Dynamic allocated memory

    ======
    SOLVED
    ======

    Hi errbody!

    Doing this school assignment, and i need some advice/help with freeing an array of dynamic allocated memory.

    Code:
    void freeMem(game *GoL){
            int i,j;
    
            for(i = 0; i < (*GoL).rows; i++){
                    for(j = 0; j < (*GoL).cols; j++){
                            free((*GoL).field[i][j].current);
                            printf("Freeing row: [%d]\n", i);
                    }
            }
            printf("--------------\nFreeing array, done!\n");
            free((*GoL).field);
    }
    Im writing a GoL game, and one of the demands of the assignments is that we have to free the dynamic allocated memory after the user has aborted the game. This code is at the end of my program and im getting this error:

    Code:
    lab3.c: In function `freeMem':
    lab3.c:249: warning: passing arg 1 of `free' makes pointer from integer without a cast
    I've been googling around for an hour or two. But most of them tells me that i need to first free every point, then every row and after that the whole array.

    So i need help/advice on why this isnt working

    Thanks!
    Last edited by TiNkiN; 10-26-2010 at 05:29 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Look at how he memory was allocated, and then just reverse it. VERY doubtful you will need to free every element of an array. Surely, one row of it would be the smallest size set that was malloc'd in a single call to the function.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Quote Originally Posted by Adak View Post
    Look at how he memory was allocated, and then just reverse it. VERY doubtful you will need to free every element of an array. Surely, one row of it would be the smallest size set that was malloc'd in a single call to the function.
    This is how my allocating function looks like:
    Code:
    cell **allocField(game *GoL){
    
            int i;
            cell **field;
    
            field = (cell **)calloc((*GoL).rows, sizeof(cell *));
            for(i = 0; i < (*GoL).rows; i++)
            {
                field[i] = (cell *)calloc((*GoL).cols, sizeof(cell));
            }
    
            return field;
    
    }
    reversing would be to write something like this right?

    Code:
    void freeMem(game *GoL){
            int i;
            cell **field;
    
            for(i = 0; i < (*GoL).rows; i++)
            {
                free(field[i]);
            }
    
            printf("\nFreeing array, done!\n");
            free(field);
    }

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Im getting segmentation Fault(core dumped) when im using the new code, any hints?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm not real confident with this topic, but imo. No. Your allocation looks fine*, but not your freeing function.

    I believe you need to pass the **field pointer, into the freeing function, not just declare one with the same name, inside it.

    Test your address of the cell **field, and see if it matches up with the address that was given to the earlier cell **field.

    If it's the same, then OK. Otherwise, you need to remove that one, and pass in the other one.

    *Gol is a pointer to game, and what is a "game"? And "cell" is a what?.
    Last edited by Adak; 10-26-2010 at 05:23 AM.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Quote Originally Posted by Adak View Post
    I'm not real confident with this topic, but imo. No. Your allocation looks fine*, but not your freeing function.

    I believe you need to pass the **field pointer, into the freeing function, not just declare one with the same name, inside it.

    Test your address of the cell **field, and see if it matches up with the address that was given to the earlier cell **field.

    If it's the same, then OK. Otherwise, you need to remove that one, and pass in the other one.

    *Gol is a pointer to game, and what is a "game"? And "cell" is a what?.
    Solved it! thanks for your help

    game and cell is struct's ive written. ty

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by TiNkiN View Post
    Im getting segmentation Fault(core dumped) when im using the new code, any hints?
    That's probably because in your freemem function Field is undefined.

    If construct memory as you've shown... you need to deconstruct it as you've shown.
    Last edited by CommonTater; 10-26-2010 at 05:30 AM. Reason: (sigh)... always a penny short and a minute late.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe it has to be field - well he could have changed the names, of course, also.

    Code:
    field = (cell **)calloc((*GoL).rows, sizeof(cell *));

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    this is the code that worked for me. Thanks for all your help!

    Code:
    void freeMem(cell **field, game *GoL){
            int i;
    
            for(i = 0; i < (*GoL).rows; i++) {
                    free(field[i]);
            }
            free(field);
    
    }

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Good grief! I got a dynamic memory problem ID'd - at 3 a.m.!!

    I should go buy a lottery ticket!!

    You're very welcome.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    Good grief! I got a dynamic memory problem ID'd - at 3 a.m.!!

    I should go buy a lottery ticket!!

    You're very welcome.
    LOL... Another midnight programmer!

    It's 6:30 here and I'm just getting my morning coffee... I'm impressed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heap corruption with dynamic memory
    By Head In Jar Man in forum C++ Programming
    Replies: 8
    Last Post: 01-14-2009, 02:58 AM
  2. Freeing memory
    By C_ntua in forum C Programming
    Replies: 17
    Last Post: 06-29-2008, 05:42 AM
  3. Dynamic Linking & Memory usage
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 06-02-2007, 10:57 PM
  4. Malloc & Calloc difference in terms of memory allocated
    By swapnaoe in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 01:57 AM
  5. 2D array in dynamic memory
    By VanJay011379 in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2003, 08:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21