如何确定提供给 free() 的地址是否是无效地址?

发布于 2024-11-17 18:02:45 字数 172 浏览 6 评论 0原文

在调用 free( ) 之前,有什么方法可以查明提供给 free( ) 的地址是否为无效地址?

我们知道,如果地址是无效地址(已释放的地址),free( ) 会创建未定义的行为。那么如何确保该地址是有效的呢?

提前致谢。

Is there any way to find out whether an address supplied to free( ) is an invalid address, before calling the free( )?

We know free( ) creates an undefined behaviour if the address is an invalid address (already freed address). So how to make sure that address is a valid one?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

奢欲 2024-11-24 18:02:45

如果你需要这样做,你就会遇到一个更大的问题——你在完成内存之前释放了内存,或者在完成内存后你没有使对内存的引用无效。您应该尝试解决指针生命周期问题,而不是尝试通过更改 free() 来阻止程序双重释放。

您可能会发现 Valgrind 可以帮助您找出问题所在。

If you need to do this, you have a bigger problem -- you have free'd your memory before you've finished with it, or you've not invalidated references to memory when you've finished with it. Rather than trying to stop your program double-freeing by changing free(), you should be trying to fix your pointer lifetime issues.

You may find that Valgrind can help you to work out where things are going wrong.

南烟 2024-11-24 18:02:45

无效地址的 free() 行为未定义,因为它是不可预测的。在像 C 这样的低级语言中,您无法判断地址是“有效”还是“无效”。地址无效的原因有很多种。 “已释放”只是其中一种情况,其他情况包括:地址未由 malloc 分配、数据实际上不是地址(例如,某些整数或字符串转换为指针)、数据损坏等。

如果您对“已经释放'的情况下,只有您可以尝试跟踪每个内存分配和释放(例如,通过使用自定义 malloc()free() 包装器),但这可能只会使你的代码更加复杂甚至更容易出错。

简单回答:不,没有办法检查地址是否无效。当您根本不关心内存地址时,接受它或切换到更高级的语言(例如Python)。

The behaviour on free() for invalid address is undefined, because it is unpredictable. In a low-level language like C you cannot say if address is 'valid' or 'invalid'. And there are many ways a address may be invalid. 'Already freed' is only one of the cases other include: address not allocated by malloc, the data is not actually an address (e.g. some integer or character string casted to pointer), data corruption, etc.

If you are interested in the 'already freed' case only you could try to track every memory allocation and deallocation (e.g. by using a custom malloc() and free() wrapper) but this would probably only make your code more complicated and even more error-prone.

Simple answer: no, there is no way to check if the address is invalid. Live with it or switch to a higher level language (like e.g. Python), when you don't care about memory addresses at all.

平安喜乐 2024-11-24 18:02:45

为了提供更多帮助,我们确实需要了解您的预期用例。

如果您使用相同的指针有时引用 malloc 获得的内存,有时引用字符串文字或自动变量,那么您应该简单地在指针旁边存储一个标志,指示它是否指向由malloc获得的东西。或者,您可以简单地存储名为 ptr_to_free 的指针的第二个副本,当数据不需要释放时,该副本为 NULL。这还有一个优点,即您的“主指针”可以“移动”并指向对象的不同部分,因为您将保持原始/基指针单独释放。

如果只是为了避免双重释放问题,您只需要修复您的代码,这样它们就不会发生。在释放指针所指向的内容后立即将其设置为 NULL 是一个草率的技巧,通常可以让您完成大部分工作,但您可能有更大的设计问题需要重新考虑......

No.

In order to be more helpful we really need to know your intended usage case.

If it's that you're using the same pointers to sometimes reference memory obtained by malloc, and at other times reference string literals or automatic variables, then you should simply store a flag alongside the pointer that indicates whether it points to something obtained by malloc. Or you could simply store a second copy of the pointer called ptr_to_free that's NULL when the data does not need freeing. This also has the advantage that your "main pointer" could "move around" and point to different parts of the object, since you'd be keeping the original/base pointer to free separately.

If it's just to avoid double-free issues, you just need to fix your code so they don't happen. Setting a pointer to NULL just after you free what it points to is a sloppy trick that usually gets you most of the way there, but you probably have bigger design issues to rethink...

×眷恋的温暖 2024-11-24 18:02:45

正如其他人所建议的,跟踪指针是了解内存是否已释放的好方法。

我还建议以下几点。

  1. 声明指针时将其初始化为 NULL
  2. 释放后,再次将其初始化为 NULL
  3. 在尝试释放之前检查指针是否不为 NULL。

As others have suggested, tracking a pointer is good way of knowing if memory has been freed or not.

I would also suggest the following pointers.

  1. Initialiase the pointer to NULL while you declare it
  2. Once freed you initialise it to NULL again
  3. Check if pointer is not NULL before trying to free.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文