Thread: which is the proper way to use free in this example?

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    9

    which is the proper way to use free in this example?

    Hello,

    Suppose I dynamically create memory for an array in an external function. When using free later, I'm not sure if I should just use free once on the entire array object, or if I am supposed to iterate through the array an free each element. Or if I am supposed to do something else entirely.

    Version 1:
    Code:
    int * array_malloc_internal(int size) {  
        int *result = (int*)malloc(size * sizeof(int));
        return result;  
    }
    
    
    int main( int argc, char* argv[] ) {
        
        int SIZE = 5;
        int *my_array = array_malloc_internal(SIZE);
        free(my_array);
        return 0;
    }

    Version 2:
    Code:
    int * array_malloc_internal(int size) {  
        int *result = (int*)malloc(size * sizeof(int));
        return result;  
    }
    
    
    int main( int argc, char* argv[] ) {
        
        int SIZE = 5;
        int *my_array = array_malloc_internal(SIZE);
    
        for (int i = 0; i < SIZE; i++) {
            free(my_array[i]);
        }
        
        return 0;
    }
    I know the external function is not "doing anything" other than allocating memory. I'm trying to use the most basic example possible, because it is the proper freeing of the array I am interested in. I'm guessing it's the first one. The first one has a one free for every one malloc. And my_array inside of main is probably thought of as one big data space in memory once the helper function was called and returned. So the program probably knows to free the entire space of the array. But I could be wrong.

    Thank you.
    Last edited by c_seeker; 01-16-2014 at 01:45 AM. Reason: refinement

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One free per malloc, only.

  3. #3
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    In this particular case you only need one free. However, if your malloced array was not holding just ints but pointers, and each one of those pointers was pointing to some allocated memory (a hash table with linked-list chaining is a typical example here) then you should free each array[i] separately before freeing array itself.

    I hope it made sense.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You don't need to cast malloc: FAQ > Casting malloc - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 12-28-2012, 04:07 PM
  2. new license-free lock-free data structure library published
    By Toby Douglass in forum Projects and Job Recruitment
    Replies: 19
    Last Post: 12-22-2009, 02:33 AM
  3. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  4. What is Proper?
    By Flakster in forum C++ Programming
    Replies: 12
    Last Post: 10-04-2005, 11:56 AM
  5. What's cooler than free boobs? 5 free assembly books from AMD.
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 02-13-2003, 08:22 PM