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 1 of 6.
Anjali said:
1 decade ago
Well , actually p holds the starting address of the array.......free funtion will delete the memory which is allocated for that array.......say array is of size 10 and it is of integer type so it should require 20 bytes of memory as "int" occupies 2 bytes of memory (ofcourse it depends on compiler). So when we free the pointer it doesn't mean that we are deleting the pointer . We are deleting the memory which is allocated to the pointer i.e, 20 bytes of memory (according to my example).........so p has the address 1314 itself.
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.
Mohit Dwivedi said:
1 decade ago
When you call free() it will de-allocate the address dynamically assigned via malloc function. Here pointer p is pointing to the first address of set of 20 memory allocation which will be dynamically allocated. Now when we use free() this dynamic allocation ended but not the existence of pointer. You can allocate another set of value using the same pointer variable(p). So the address of pointer will remain the same even after using free().
Ramesh M said:
1 decade ago
After memory chunk of 20 elements allocated the pointer p holds base address of chunk (i.e, 1314) , then free (p) is done so that the memory chunk of 20 elements is available to be reused by the library function again (memory added to main memory). But after freeing is done pointer p is pointing to no where (but holds the address 1314) , so it should be made NULL, on further usage.
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.
Hossam Ahmed said:
2 years ago
@All.
Here is my explanation.
Simply:
free() removes the data stored in the location but the pointer can still point at the same location.
When trying to access it again with the pointer, it gives a Garbage value.
So, the location is free as data storing not freeing or removing or making the pointer stop pointing at the location.
Here is my explanation.
Simply:
free() removes the data stored in the location but the pointer can still point at the same location.
When trying to access it again with the pointer, it gives a Garbage value.
So, the location is free as data storing not freeing or removing or making the pointer stop pointing at the location.
(2)
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.
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.
Maheshwaran said:
1 decade ago
free() function will free the dynamic memory space for pointer of p.
But declaration of integer *p has been specified by some other address. That will not free by that free() function.
free() - It releases the dynamically allocated memory not declared memory.
But declaration of integer *p has been specified by some other address. That will not free by that free() function.
free() - It releases the dynamically allocated memory not declared memory.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers