理解免费的概念
尝试了以下代码:
#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;
}
得到的输出如下:
[root@lnxdesk Tazim]# ./a.out
154804232
154804232
154804232
为什么即使我已经完成了free(p);
,p内的地址仍然被打印? 那么free(p)
做了什么?
我想清楚地理解free/malloc的概念。任何帮助都将是有价值的。
Tried the following 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
free()
仅释放堆上的内存。它不会改变指针的值。如果您尝试打印指针指向的内存,您可能会得到某种垃圾。另外,当您调用
free
时,您给了它指针,而不是指针的地址,因此free
无法更改您的指针...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, sofree
can't change your pointer...这是未定义的行为 - 一旦您
释放
指针,存储的地址就会变得无效,并且您不能这样做任何带有它的东西 - 不仅不能取消引用它,而且甚至不能printf()
指针值。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 evenprintf()
the pointer value.您正在打印指针,即为您分配的内存区域的地址。正如我认为您所期望的那样,使用
free
释放内存区域不会将指针的地址设置为 0x00。它只是告诉操作系统该内存区域再次可用以供将来重复使用。
如果您在
free(p)
之后打印*p
,则会出现问题。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
afterfree(p)
, you would have problems.malloc() 及其同类在称为“堆”的内存存储区域中保留空间,并返回指向该保留区域的指针。因此,在上面的示例中,
p
被赋予了一个指针,该指针可能指向一个已保留供其使用的四字节内存区域(这次其地址恰好是 154804232)。当您执行*p = 10
时,您现在将整数值10
放入指向的内存中。当您执行q = p
时,您现在使q
指向保留堆内存的同一块。free()
及其同类只是取消保留内存。当您调用free()
时,您是在说“我不会再使用此内存了”。 free() 所做的只是告诉内存管理系统该内存块现在可以再次使用。 它不会改变你的指针。它只是表明内存块可用。之后,您需要确保不再使用该指针。如果您再次使用该指针,它可能会正常工作。一次。或者两次。或者一千次。基本上,它会工作得很好,直到你在其他人声称你所说的内存块是空闲的并用它做一些事情之后使用它。当这种情况发生时,坏事就会发生。请不要让不好的事情发生。
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 abovep
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 value10
into the memory pointed to. When you doq = p
you're now makingq
point to the same chunk of reserved heap memory.free()
and its ilk just unreserve the memory. When you callfree()
you're saying "I'm not going to use this memory anymore". Allfree()
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.
请记住:指针只不过是一个地址。在您的 malloc 或 free 之前、之后,它会给您相同的结果。 malloc() 唯一做的就是在该地址保留空间。 free 唯一做的就是释放它(最有可能的是,将此地址标记为可用于存储其他内容,“清理”将非常耗时)。
这就是为什么在释放之后将指针指向 NULL 是一个好主意;因为您可以确定指针是否连接到某物。
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 不会重新分配指针以指向其他内容。事实上,C标准
没有提到用指针做任何事情。这就是描述中的全部内容:
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: