Solved

What does free() actually do ?

Posted on 2003-02-27
Medium Priority
163 Views
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

Dhritiman

0
Question by:Dhritiman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 1

Expert Comment

ID: 8031922
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

LVL 8

Accepted Solution

akshayxx earned 200 total points
ID: 8032038
>>>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

LVL 12

Expert Comment

ID: 8032060
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));
}

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

Just compare the statements...

0

LVL 20

Expert Comment

ID: 10019038
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

jmcg
EE Cleanup Volunteer
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month9 days, 21 hours left to enroll