Archived

This topic is now archived and is closed to further replies.

serene

Freeing memory for three dimensional array help needed

Recommended Posts

Could anyone teach me how to free memory of a three dimensional array by using the C program? I am using the UNIX system. This is how i create the three dimensional arrays of integers int ***vals; int i,j,n; scanf("%d", &n); vals=(int***)malloc(n*sizeof(int**)); for(i=0;i=(int**)malloc(n*sizeof(int*)); for(i=0;i[edited by - serene on April 5, 2004 11:48:45 PM]

Share this post


Link to post
Share on other sites
How you free the array depends on how you allocate it. I can''t even begin to guess how you allocated yours since you didn''t even mention what programming language you are using.

Share this post


Link to post
Share on other sites
If you''re using malloc() to allocate it, you just need to use free() on every pointer that malloc() returned. Something like:

for(i=0;i<n;i++)
for(j=0;j<n;j++)
free(vals[i][j]);

for(i=0;i<n;i++)
free(vals[i]);

free(vals);


As an aside, it probably would have been better if you had just posted a reply instead of editting your original post. Usually most people don''t re-check a thread if they don''t see that the post count has changed.

Share this post


Link to post
Share on other sites