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
这篇关于检查一个指针是否指向堆上分配的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:检查一个指针是否指向堆上分配的内存
基础教程推荐
- c++ STL设置差异 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
