我可以调用一个命令来打印 malloc 数据结构吗?

发布于 2024-10-07 03:56:28 字数 125 浏览 3 评论 0原文

您好,我想知道是否有任何现成的函数可以调用来打印所有 malloc 数据结构,以便我可以看到为哪个变量分配了哪些内存?

我有这种内存损坏,当我释放一个变量时,它会抱怨,但我不知道哪个变量与它相邻。

谢谢!

Hi I wonder if there is any ready made function that I can call to print all the malloc data structures, so that I can see which memory is allocated for which variable?

I have this memory corruption, that when I free one variable it complains, but I have no idea which variable is adjacent to it.

Thanks!

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

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

发布评论

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

评论(4

倒数 2024-10-14 03:56:28

尝试在 valgrind 下运行您的程序。如果幸运的话,它会直接指出有问题的越界内存写入。 (如果你运气不好,你只会收到大量关于 C 库深处代码的虚假抱怨。)

Try running your program under valgrind. If you're lucky it'll point you right at the offending out-of-bounds memory write. (If you're unlucky you'll just get a flood of spurious complaints about code deep in the C library.)

池木 2024-10-14 03:56:28

我认为这将非常难以弄清楚您描述它的方式,因为内存中没有任何内容可以指向所属变量。

理论上,您可以遍历应用程序创建的所有对象的整个树,直到找到一个指向导致问题的位置旁边的内存的指针。

您可以使用像 gdb 这样的工具来转储您分配的内存块并亲自查看。也许您会通过查看数据来识别这些数据,这会向您指出导致问题的代码。

最好的选择是使用像 electricfence 这样的工具,当您的代码试图破坏内存时,它会立即终止您的应用程序。

I think this would be extremely difficult to figure out the way you describe it, since there is nothing in the memory that points back to the owning variables.

You could in theory traverse the entire tree of all objects your application created, until you find a pointer that points to memory next to the spot that causes trouble.

You could use a tool like gdb to dump the chunk of memory around your allocation and see for yourself. Maybe you will recognize the data by looking at them and that would point you to the code that causes trouble.

Your best bet would be to use a tool like electricfence that would kill your application instantly when your code attempts to corrupt the memory.

过期以后 2024-10-14 03:56:28

我不知道您可以从程序中调用任何函数,但您可以使用调试器来执行此操作。尝试 https://github.com/cloudburst/libheap 或在 Windbg 的 Win32 下使用 '!heap'命令。

I don't know of any function you can call from within your program but you can do this with a debugger. Try https://github.com/cloudburst/libheap or under Win32 in windbg use the '!heap' command.

℡寂寞咖啡 2024-10-14 03:56:28

我们的 CheckPointer 工具可能可以找到损坏内存的确切位置。

大多数内存检查工具在数据周围都有某种“围栏”来检测错误的访问。这种栅栏的粒度比被栅栏的对象大得多;如果您访问对象外部但在栅栏内部,则不会检测到错误。例如,Valgrind 对堆栈帧一无所知,因此它无法检测对超出范围并被另一个堆栈帧覆盖的堆栈帧的访问。 CheckPointer可以

CheckPointer 跟踪每个存储块(堆、堆栈、结构的一部分)和每次访问的确切分配。它确切地知道您何时超出为存储实体预留的确切空间(例如,如果您到达嵌入在结构中间的数组的末尾)。因此它可以提供更好的检查。

CheckPointer 还将提供所有仍分配存储的执行后转储;当然,您可以在代码中的任意位置调用该转储过程作为调试辅助。

Our CheckPointer tool can likely find the exact place where you corrupt the memory.

Most memory checking tools have some kind of "fence" around your data to detect a bad access. Such fences have granularitly considerably larger than the object being fenced; if you access outside the object but inside the fence, the error isn't detected. For instance, Valgrind has no clue about stack frames, so it can't detect an access to a stack frame which has gone out of scope and been overwritten by another. CheckPointer can.

CheckPointer tracks the exact allocation of each block of store (heap, stack, part-of-struct) and each access. It knows exactly when you've stepped outside the exact space set aside for the storage entity (e.g., if you reach off the end of an array embedded in the middle of a struct). It can thus provide much better checking.

CheckPointer will also provide a post-execution dump of all still-allocated storage; of course, you could call that dump procedure at an arbitrary place in your code as a debugging aid.

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