Understanding concept of free
Tried the follo开发者_JAVA技巧wing code :
#include<stdio.h>
int main()
{
int *p,*q;
p = (int *)malloc(sizeof(int));
*p =10;
q = p;
printf("%u \n",p);
printf("%u \n",q);
free(p);
printf("%u \n",p);
return 0;
}
The output got is as follows :
[root@lnxdesk Tazim]# ./a.out
154804232
154804232
154804232
Why is that address inside p is still printed even if I have done free(p);
?
What has free(p)
done then?
I want to understand the concept of free/malloc clearly. Any help will be valuable.
free()
only frees the memory on the heap. It does not change the value of your pointer. If you tried to print the memory pointed by your pointer, you'll probably get some kind of garbage.
Also, when you called free
, you gave it the pointer, not the address to your pointer, so free
can't change your pointer...
That's undefined behavior - once you've free
d the pointer the address stored becomes invalid and you can't do anything with it - not only you can't dereference it, but you can't even printf()
the pointer value.
You are printing the pointers, i.e. the address of the memory zones allocated for you ints. Freeing a memory zone with free
does not set the pointer's address to 0x00 as I think you expect.
It just tells the OS that the memory zone is available again for future re-use.
If you were printing *p
after free(p)
, you would have problems.
malloc()
and its ilk reserve space in a memory storage area called the "heap" and return a pointer to that reserved area. So in your sample above p
is given a pointer to, probably, a four-byte memory region that has been reserved for its use (whose address happens to be 154804232 this time around). When you do *p = 10
you are now placing the integer value 10
into the memory pointed to. When you do q = p
you're now making q
point to the same chunk of reserved heap memory.
free()
and its ilk just unreserve the memory. When you call free()
you're saying "I'm not going to use this memory anymore". All free()
does is tell the memory management system that the block of memory is now available for use once again. It emphatically does not change your pointer. It just signals that the block of memory is available. After that it is up to you to ensure that you do not use that pointer again.
If you do use that pointer again it may work fine. Once. Or twice. Or a thousand times. It'll work fine, basically, until you use it after someone else claims that memory block you've said is free and does something with it. When that transpires, Bad Things Happen<tm>. Please don't make bad things happen.
Remember : a pointer is nothing but an address. Before, after your malloc, or free, it'll give you the same result. The only thing that malloc() does is reserve space at this address. The only thing that free does is release it (most probably, mark this address as usable to store other things, "cleaning" would be time consuming). This is why putting your pointer to NULL after a free is a good idea ; because you can be sure if the pointer is connected to something or not.
free does not reassign the pointer to point to something else. In fact, the C standard does not mention anything be done with the pointer. This is all it says in the description:
The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined
精彩评论