Run this below program
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p=malloc(sizeof(int)*7);
printf("%d ",p);
free(p);
printf("%d ",p[2]);
int *m=calloc(8,sizeof(int));
m[2]=100;
printf("%d ",m);
printf("%d ",m[2]);
m[6]=90;
printf("%d ",m[6]);
free(m);
realloc(m,4);
printf("%d ",m);
printf("%d ",m[2]);
printf("%d ",m[6]);
free(m);
}
First of all it will tell
1) syntax of free, malloc, realloc,calloc
2) If you pay attention we can see even after freeing p , we can still access p[2].
The reason being " when we malloc or calloc some memory" compiler stores some additional information regarding that memory...size , initial values" , and it returns back actual RAM address.
When free is called, all it does is removing that information. Then again depending on compiler free can set the pointer to NULL, can all delete the value stored in that RAM address.
But it seems GCC doesn't does that, therefore since we have address of physical address we can still access what is in it. Therefore whenever you call free in your code, REMEMBER to MAKE pointer NULL after that, else memory corruption can happen.
This also answers the question which is asked many times in interview , that how does FREE knows how much memory to free , when we don't pass any size to that.
Also the below code will give run-time error. Since with first free we have deleted any information regarding the pointer M. hence at second free compiler returns error.
int main()
{
int *M=(int *)malloc(sizeof(int));
free(M);
free(M);
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p=malloc(sizeof(int)*7);
printf("%d ",p);
free(p);
printf("%d ",p[2]);
int *m=calloc(8,sizeof(int));
m[2]=100;
printf("%d ",m);
printf("%d ",m[2]);
m[6]=90;
printf("%d ",m[6]);
free(m);
realloc(m,4);
printf("%d ",m);
printf("%d ",m[2]);
printf("%d ",m[6]);
free(m);
}
First of all it will tell
1) syntax of free, malloc, realloc,calloc
2) If you pay attention we can see even after freeing p , we can still access p[2].
The reason being " when we malloc or calloc some memory" compiler stores some additional information regarding that memory...size , initial values" , and it returns back actual RAM address.
When free is called, all it does is removing that information. Then again depending on compiler free can set the pointer to NULL, can all delete the value stored in that RAM address.
But it seems GCC doesn't does that, therefore since we have address of physical address we can still access what is in it. Therefore whenever you call free in your code, REMEMBER to MAKE pointer NULL after that, else memory corruption can happen.
This also answers the question which is asked many times in interview , that how does FREE knows how much memory to free , when we don't pass any size to that.
Also the below code will give run-time error. Since with first free we have deleted any information regarding the pointer M. hence at second free compiler returns error.
int main()
{
int *M=(int *)malloc(sizeof(int));
free(M);
free(M);
}
No comments:
Post a Comment