检查一个指针指向分配内存堆 [英] Check if a pointer points to allocated memory on the heap
问题描述
我想知道,如果一个指针指向一块内存使用malloc / new分配。我知道,对于一个任意地址的答案是不,你不能,但我不认为这是可以覆盖的malloc /免费,并保持分配的内存范围的轨道。
I want to know if a pointer points to a piece of memory allocated with malloc/new. I realize that the answer for an arbitrary address is "No you can't" but I do think it is possible to override malloc/free and keep track of allocated memory ranges.
你知道一个存储器管理程序库提供这种特定的工具?结果
你知道什么生产code?
Do you know a memory management library providing this specific tool?
Do you know something for production code?
Valgrind的是伟大的,但实在是太仪表(慢)和威尔说,我们不希望使用Valgrind的这样的(使得软崩溃是不够好)。结果
挡泥板是一个很好的解决方案,而是致力于GCC,和可悲的是,一查不是简单地返回一个布尔(见我下面回答)。结果
请注意,检查存储器的写入法律是安全问题。因此,寻找业绩的动机。
Valgrind is great, but it is too much instrumentation (slow) and as Will said we don't want to use Valgrind like this (making the soft crash is good enough).
Mudflap is a very good solution, but dedicated to GCC, and sadly, a check does not simply return a boolean (see my answer below).
Note that checking that memory writes are legal is a security issue. So looking for performance is motivated.
推荐答案
有做到这一点没有标准方法,但各种malloc调试工具可能有这样做的一种方式。例如,如果您使用的valgrind ,则可以使用 VALGRIND_CHECK_MEM_IS_ADDRESSABLE
来检查这一点,相关的东西
There's no standard way to do this, but various malloc debugging tools may have a way of doing it. For example, if you use valgrind, you can use VALGRIND_CHECK_MEM_IS_ADDRESSABLE
to check this and related things
这篇关于检查一个指针指向分配内存堆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!