• C

What does free() actually do ?

I create a double dimensioned array of int elements and allocate memory to it : -

int **arr;
int x,y;
x = 4;
y = 5;
arr = malloc(x*(sizeof(*arr)));
for (i=0;i<x;i++)
{
arr[i] = malloc(y*sizeof(**arr));
}


Then I enter the values in the array.
And then ... free the array : -

for (i=0;i<x;i++) free(arr[i]);
free(arr);

After freeing i print the array and the i still get the values.
Does free() wipes off the allocated memory ?
Does free() sets the pointer to NULL ?
Or it justs flags the allocated memory in a way so that it can be allocated to some other pointers ?

Kindly help
thanks already.

Dhritiman


DhritimanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

umangjoshiCommented:
pls declare like this

int *nos[10];
int no_of_cols = 5;
nos[0] = (int *)malloc(no_of_cols * sizeof(int));
nos[1] = (int *)malloc(no_of_cols * sizeof(int));
0
akshayxxCommented:
>>>After freeing i print the array and the i still get the values.

yes u can because what free does is that it marks the memory pointed to .be available for further allocation calls...

>>>Does free() wipes off the allocated memory ?

 it doenst wipe of .. just marks it as free for allocation ..

>>>Does free() sets the pointer to NULL ?

NO it doesnt ..

>>>Or it justs flags the allocated memory in a way so that it can be allocated to some other pointers ?

yes you got it here
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rajeev_devinCommented:
No the free does not sets memory to NULL. Infact it just releases the allocated memory to the operating system. So that the released memory can be allocated next for another request. Another thing is that you have not done any explicit typecast. In all compilers it will not compile. Try to do typecast. Your some statements, I think, is not correct

     int x,y;
     x = 4;
     y = 6;
     arr = (char **) malloc(x*sizeof(char *));
     for (i=0;i<x;i++)
     {
          arr[i] = (char *) malloc(y*sizeof(char));
     }
// Do your work here
 
     for (i=0;i<x;i++) free(arr[i]);
     free(arr);

Just compare the statements...
Give further comments...

0
jmcgOwnerCommented:
Nothing has happened on this question in over 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by akshayxx.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.