Check if a pointer points to allocated memory on the heap(检查一个指针是否指向堆上分配的内存)
问题描述
我想知道一个指针是否指向一块用malloc/new分配的内存.我意识到任意地址的答案是不,你不能",但我确实认为可以覆盖 malloc/free 并跟踪分配的内存范围.
您知道提供此特定工具的内存管理库吗?
您了解生产代码吗?
Valgrind 很棒,但仪器太多(慢),正如 Will 所说,我们不想像这样使用 Valgrind(使软崩溃就足够了).
Mudflap 是一个非常好的解决方案,但专用于 GCC,遗憾的是,检查并没有简单地返回一个布尔值(见下面我的回答).
请注意,检查内存写入是否合法是一个安全问题.所以追求性能是有动力的.
没有标准的方法可以做到这一点,但各种 malloc 调试工具可能有办法做到这一点.例如,如果你使用valgrind,你可以使用VALGRIND_CHECK_MEM_IS_ADDRESSABLE
来检查这个和相关的事情>
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.
Do you know a memory management library providing this specific tool?
Do you know something for production code?
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.
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
这篇关于检查一个指针是否指向堆上分配的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:检查一个指针是否指向堆上分配的内存


基础教程推荐
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16