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 3 of 6.
Chandrabhan said:
1 decade ago
There is no any option in output list. O/P = 145711224.
Akarsh bajpai said:
1 decade ago
@Arnab Bhattacharya.
It is actual base address of the pointer allocated during runtime in memory ;here we are assuming it is 1314.
It is actual base address of the pointer allocated during runtime in memory ;here we are assuming it is 1314.
Akarsh bajpai said:
1 decade ago
When we call free() the memory allocated to the first 20 elements with the help of pointer gets deleted but not the existence of the base address of the pointer. Pointer will always remain there you can reallocate memory with the help of pointer, or use pointer in any respect you want.
Anshika said:
1 decade ago
Actually first it allocates 80 bytes to p. And then it free the memory of p. That means only allocated memory to it has been destroyed not its base address which is still 1314. So on printing the address of p it gives 1314 instead of giving garbage value.
Abhijit said:
1 decade ago
I'm still confused.
Why its taking value 1314 only why not 1316 ?
Why its taking value 1314 only why not 1316 ?
AMEERUL said:
1 decade ago
Here p become dangling pointer it will totally depends on OS if after freeing memory region os allocate this region to another variable or function then it will give a random or garbage address. Otherwise 1314.
Shreyans said:
1 decade ago
Using free() one time leads to Memory leak, i.e., in this case (hence the pointer still pointing to 1314 location that is became DanglingPointer).
Whereas using free() twice leads to memory/program crash.
Whereas using free() twice leads to memory/program crash.
Prasad said:
1 decade ago
Eventhough the array is deleted the pointer still pointing to memory location in heap area so we must assign null value to dangling pointer.
Manisha said:
1 decade ago
The 'free' does not delete the pointer. The dangling pointer remains.
Apeksha said:
1 decade ago
This kind of pointer is known as "Dangling pointer".
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers