C Programming - Memory Allocation - Discussion
Discussion Forum : Memory Allocation - Find Output of Program (Q.No. 1)
1.
What will be the output of the program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p;
p = (int *)malloc(20); /* Assume p has address of 1314 */
free(p);
printf("%u", p);
return 0;
}
Discussion:
57 comments Page 4 of 6.
Prasenjeet Mahtha said:
1 decade ago
free() function clears the memory space. It does not remove the declared pointer.
Arnab Bhattacharya said:
1 decade ago
"134520952"
That's the answer I got using this online compiler...!!!
Please explain...!!!!
That's the answer I got using this online compiler...!!!
Please explain...!!!!
The Guide said:
1 decade ago
free() deletes the memory that is pointed by p but not the address of p i.e in the above example the free() deletes a memory that p points to an integer.
Rinkoo said:
1 decade ago
Saradhi & boobal. I like your answer.
Hemant singh said:
1 decade ago
We are assigning the address to p not the value where as the free() function clear the content of the memory location not the address.
MaheshBabu said:
1 decade ago
Anjali you are giving nice explenation thanks for giving me such type of explenation.
Riyaz said:
1 decade ago
Yes anjali you are very correct. Thanks for your explanation.
Sweety said:
1 decade ago
Give me clear explanation.
Ashu said:
1 decade ago
Actually the pointer is not deleted it is the space which is deleted.
Bharrath said:
1 decade ago
Yes, the free () function will free the memory of p array but not the pointer so still it points the address of p that's why it prints 1314 as assumed in the program.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers