理解免费的概念

发布于 2024-10-25 06:04:46 字数 521 浏览 3 评论 0原文

尝试了以下代码:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

深者入戏 2024-11-01 06:04:46

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, so free can't change your pointer...

雨落星ぅ辰 2024-11-01 06:04:46

这是未定义的行为 - 一旦您释放指针,存储的地址就会变得无效,并且您不能这样做任何带有它的东西 - 不仅不能取消引用它,而且甚至不能 printf() 指针值。

That's undefined behavior - once you've freed 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.

紫竹語嫣☆ 2024-11-01 06:04:46

您正在打印指针,即为您分配的内存区域的地址。正如我认为您所期望的那样,使用 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 after free(p), you would have problems.

情话墙 2024-11-01 06:04:46

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 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.

活泼老夫 2024-11-01 06:04:46

请记住:指针只不过是一个地址。在您的 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.

轮廓§ 2024-11-01 06:04:46

free 不会重新分配指针以指向其他内容。事实上,C标准
没有提到用指针做任何事情。这就是描述中的全部内容:

free函数导致空间
ptr 指向要释放的,
也就是说,可用于进一步
分配。如果 ptr 是空指针,
没有任何动作发生。否则,如果
参数与指针不匹配
之前由 calloc 返回,
malloc 或 realloc 函数,或者如果
空间已被调用释放
释放或重新分配,行为是
未定义

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文