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 4 of 6.

Kuldeep said:   10 years ago
This is also called as dangling pointer. To avoid this situation you must assign p=NULL.

Muzammil said:   9 years ago
The free is a function which will free the data allocated to the memory, but not memory.

MaheshBabu said:   1 decade ago
Anjali you are giving nice explenation thanks for giving me such type of explenation.

Gautam said:   1 decade ago
When you call free(p) then it will deallocate the address 1314. Please explain it.

Prasenjeet Mahtha said:   1 decade ago
free() function clears the memory space. It does not remove the declared pointer.

Sarath said:   2 years ago
@All.

Here;
printf("%u",p) will give an error.

i.e change to printf("%p",p).

Kartheek said:   9 years ago
Because of dangling pointer concept. After p= null only to avoid that.

Manisha said:   1 decade ago
The 'free' does not delete the pointer. The dangling pointer remains.

Ashu said:   1 decade ago
Actually the pointer is not deleted it is the space which is deleted.

Abhijit said:   1 decade ago
I'm still confused.

Why its taking value 1314 only why not 1316 ?


Post your comments here:

Your comments will be displayed after verification.