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.
Robert said:
9 years ago
free(p);
p = NULL;
This is defensive coding style against dangling pointers bugs.
Also on alloc, check the pointer.
if (p != NULL)
p = NULL;
This is defensive coding style against dangling pointers bugs.
Also on alloc, check the pointer.
if (p != NULL)
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.
Vikas Sharma said:
1 decade ago
Here, memory allocated by malloc function gets deleted, but the pointer still holds the base address of the deleted memory block.
Nagarjuna said:
1 decade ago
Free (p) will leads to an output of garbage if it is of dangling pointer to be assumed other wise 1314 which is address of p.
Sarvajeet Suman said:
10 years ago
C compiler provided by IndiaBix online getting random number instead of 1314. Please check it out even in my GCC compiler.
Minakshi said:
1 decade ago
I think the answer should be a garbage collection. Because after free memory pointer points to garbage collection.
Saradhi said:
2 decades ago
free(); it delets that arry where it points. array is deleted but not pointer. so, print the address of p 1314
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...!!!!
Deepak said:
1 decade ago
Here the space allocated (pointed by pointer) is deleted but the pointer points to same location.
Raj said:
6 years ago
The correct answer should be garbage value. Once memory free the value will be garbage value.
(3)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers