Let's say we want to free the total memory used by a linked list in a program, I generally do it like this

``````typedef struct linked_list
{
int some_data;
}NODE;

int free_memory(NODE * head)
{
NODE * temp = NULL;

return 0;       //  FAILURE IN FREEING MEMORY
else
{
{
free(temp);
}
}

return 1;           //  SUCCESS IN FREEING MEMORY
}``````

Now suppose I've created an array

``````int * create_array(int size_of_array)
{
int * array = (int *)calloc(size_of_array,sizeof(int));

return array;
}``````

Rarely have I seen books freeing memory but sometimes I've seen this

``````# define SIZE 5

int * create_array( int );

int main()
{
int * my_array = create_array(SIZE);

//  SOME OPERATION HAS BEEN DONE ON THE ARRAY
//  NOW FREEING MEMORY

free(my_array);

return 0;
}``````

But I free memory like this

``````# define SIZE 5

int * create_array( int );

int main()
{
int * my_array = create_array(SIZE);

//  SOME OPERATION HAS BEEN DONE ON THE ARRAY
//  NOW FREEING MEMORY

free_memory(my_array, SIZE);

return 0;
}

int free_memory (int * array, int size_of_array)
{
if(array == NULL)
return 0;
else
{
int i;
for(i = size_of_array - 1; i >= 0; i--)
{
free(array[i]);
}
}

return 1;
}``````

So in a nut-shell, when we are freeing a linked list, we are freeing each node separately, but when we are freeing an array, the books are freeing only the first address.

Why this disparity?

(I'm sorry if this is a n00b question :P but i need to know)

The rule is -- one free() call for each malloc() or calloc() call. You can not free() something that was never malloced. In the case of the linked list, each node of the list was allocated by calling malloc(), therefore each node must be destroyed by calling free(). In the case of the array, there was only one call to malloc(), therefore there need be only one call to free().

`int * my_array = (int *)calloc(SIZE,sizeof(int));` `[ ][ ][ ][ ][ ]` // SOME INPUT OF NUMBERS `[1][2][3][4][5]` `free(my_array);` But each of the array elements individually have an address
So when we `free(my_array)` , all of them dissapear together `[poof!][poof!][poof!][poof!][poof!]` Is this what happens?

Or this `[poof!] <- first address` Then what happens to the rest of the elements?

When you call malloc() or calloc() you have to tell it how many bytes of memory you want allocated. In the case of the array, you told it you wanted to allocate `SIZE * sizeof(int)` number of bytes. When SIZE is defined as 5 and sizeof(int) is 4 then free() will allocate 4*5 = 20 bytes of memory. malloc() searches its list of free memory and returns a pointer to some block of consecutive bytes of memory that is at least (meaning it could be larger) 20 bytes. So when you call free() you are telling it to put that block of 20 bytes back into the free memory pool so that it can be re-used again. The memory doesn't just magically disappear -- the memory is still there its just that your program can not use it again until the program calls malloc() again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.