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.
Muzammil said:
9 years ago
The free is a function which will free the data allocated to the memory, but not memory.
Ch .suresh royal said:
9 years ago
Explanation:
free(p); -------> Means deallocating memory pointed by the address 1314, but its address still present (1314) NOTE: REFERS TO DANGLING POINTER;
I) in printf, pritting the address so it will print (1314).
free(p); -------> Means deallocating memory pointed by the address 1314, but its address still present (1314) NOTE: REFERS TO DANGLING POINTER;
I) in printf, pritting the address so it will print (1314).
Nitesh Singh said:
9 years ago
According to me, the right ans is [D].
Because printf("%u", p);
print the address in p(pointer) location is not fix.
At my first execution the ans >17190928
Ans(2)=>33681424 and third-time different answer.
Because printf("%u", p);
print the address in p(pointer) location is not fix.
At my first execution the ans >17190928
Ans(2)=>33681424 and third-time different answer.
AnubisYe said:
9 years ago
/* Assume p has address of 1314 */
Please explain this part.
Please explain this part.
Ganesh gs said:
8 years ago
So here initially p has an address of 1314.
And we're assigning some memory dynamically for the pointer p which will pount to the memory allocated. While looking into the next step we're deallocating the memory so that the memory won't exist but the pointer of address 1314 exists. And therefore while printing it, it gives us the address.
And we're assigning some memory dynamically for the pointer p which will pount to the memory allocated. While looking into the next step we're deallocating the memory so that the memory won't exist but the pointer of address 1314 exists. And therefore while printing it, it gives us the address.
Vijay said:
8 years ago
Please explain the program.
Hrishikesh Dabir said:
8 years ago
@ALL.
The answer is A. Because in this case the address allocated is 1314. But when you actually compile it some address say 123456 is allocated for the first time.
Now. When you free, the memory allocated is freed but the pointer is still not null. So it still has the address 123456. And when you print it it will print 123456.
Now when you compile it for the second time. Some other addresses say 98765 is allowed. And all the same steps again. So yep, It is not random address.
The answer is A. Because in this case the address allocated is 1314. But when you actually compile it some address say 123456 is allocated for the first time.
Now. When you free, the memory allocated is freed but the pointer is still not null. So it still has the address 123456. And when you print it it will print 123456.
Now when you compile it for the second time. Some other addresses say 98765 is allowed. And all the same steps again. So yep, It is not random address.
Vss said:
3 years ago
The operator free() only frees the memory address from the pointer variable and returns the address to the operating system for re-use, but the pointer variable (p in this case) still points to the same old address.
FYI, free() is not an operator, it's a function.
FYI, free() is not an operator, it's a function.
Sarath said:
2 years ago
@All.
Here;
printf("%u",p) will give an error.
i.e change to printf("%p",p).
Here;
printf("%u",p) will give an error.
i.e change to printf("%p",p).
Boobal said:
1 decade ago
Free will deallocate the memory but the address of p still points to the same (invalid) location and not the null value. The dereferencing of the pointer will give unpredictable values.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers