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;
}
1314
Garbage value
1316
Random address
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
57 comments Page 1 of 6.

Dilpreet said:   2 decades ago
Please give the explanation.

Prajesh Bhushan said:   2 decades ago
Please help me understand the logic behind !

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

Shashank said:   2 decades ago
Saradhi is right. I checked in turboc.

Anshul said:   1 decade ago
This is not recommendable as it leads to dangling pointer.

Omkar kuchekar said:   1 decade ago
Anyone expert knows answer?

Bhargavi said:   1 decade ago
Give me clear explanation.

Premnath.N said:   1 decade ago
We have declared the function free(p) ; but we didn't define that function, so it will skip that line and print the address of p. If we define the given function means it will return the value.

Ankit said:   1 decade ago
As we free the address allocated how the meory is allcoated ?

Deepak said:   1 decade ago
Here the space allocated (pointed by pointer) is deleted but the pointer points to same location.


Post your comments here:

Your comments will be displayed after verification.