Click here to Skip to main content
15,670,482 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm studing C programming for quiet few months now but every time I get stack in one problem. What happens if we want to free a 2d array. I know that I should use a for loop to free each row and column but I don't know if I do it right.

So this is my example.

I have an array wich is declared with int

    int c = 1000;
int array[c][2];

and here's the free function:
for (int i = 0; i < c; i++) 

When I try to compile it with gcc I get the following errors:

C:\Users\George\Desktop>gcc -std=c99 per2.c -o per2.exe
per2.c: In function 'main':
per2.c:86:2: warning: implicit declaration of function 'free' [-Wimplicit-functi
per2.c:86:2: warning: incompatible implicit declaration of built-in function 'fr
ee' [enabled by default]
per2.c:87:2: warning: incompatible implicit declaration of built-in function 'fr
ee' [enabled by default]

Is there any problem and what is the standart formula to free a 2c array (static and dynamic)

1 solution

The array is declared static on the stack and you therefor don't need to (or actually can't) free it.

You need to free if you used malloc or calloc.

Good luck!
Share this answer
CPallini 17-Dec-14 9:21am    
[no name] 17-Dec-14 9:45am    
O.o so my array don't need to be free? really? wow ok then thank you
E.F. Nijboer 17-Dec-14 10:18am    
Correct. But, you need to be carefull with static declared arrays because you could get a stack overflow exception id the array is too big. In that case you need to allocate the array on the heap and... with that comes the responsibility to free it after.
[no name] 17-Dec-14 14:49pm    
OK I inderstant. Thank you very much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900