使用gcc / g ++ / gdb / valgrind进行调试时的魔术数字?

编程入门 行业动态 更新时间:2024-10-26 07:23:42
本文介绍了使用gcc / g ++ / gdb / valgrind进行调试时的魔术数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 微软的Visual C ++如果没有被程序员自己初始化,就会用'幻数'填充内存。这有助于调试未初始化的内存。 (在Visual Studio C ++中,什么是内存分配表示?, 0xDEADBEEF与NULL )

使用linux GNU工具(g ++ / gdb)时是否有类似的功能?

谢谢!

运算符new 来将分配设置为您的首选字节模式:

void * operator new(size_t size) { void * mem = malloc(size); if(!mem){ throw std :: bad_alloc(); } memset(mem,0xEE,size); 返回mem; $ / code>

你可以在这里看到完整的GCC实现: github/gcc -mirror / gcc / blob / master / libstdc%2B%2B-v3 / libsupc%2B%2B / new_op.cc ,以防你想更密切地镜像它。

这适用于任何使用默认C ++分配器的东西,但不适用于使用普通旧 malloc()的东西。如果你需要直接从 malloc()初始化内存,你可以重写它,但是执行它的机制是不同的:你可以使用链接器的 - wrap 选项来操作符号表,并让您覆盖 malloc()。那么你当然不需要重载 operator new 。完整的方法在这里回答: stackoverflow/a/3662951/4323

Microsoft's Visual C++ fills memory with 'magic numbers' if it hasn't been initialized by the programmer itself. This helps with debugging of uninitialized memory. (In Visual Studio C++, what are the memory allocation representations?, 0xDEADBEEF vs. NULL)

Is there a similar function when using linux GNU tools (g++/gdb)?

Thanks!

解决方案

You can override the C++ operator new to set allocations to your preferred byte pattern:

void* operator new(size_t size) { void* mem = malloc(size); if (!mem) { throw std::bad_alloc(); } memset(mem, 0xEE, size); return mem; }

You can see the full GCC implementation here: github/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/libsupc%2B%2B/new_op.cc in case you want to mirror it more closely.

That will work for anything using the default C++ allocators, but not for things using regular old malloc(). If you need to initialize memory from malloc() directly, you can override that too, but the mechanism to do it is different: you can use the linker's --wrap option to manipulate the symbol table and let you override malloc(). Then you don't need to overload operator new of course. The full approach is illustrated in an answer here: stackoverflow/a/3662951/4323

更多推荐

使用gcc / g ++ / gdb / valgrind进行调试时的魔术数字?

本文发布于:2023-11-12 07:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580853.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:魔术   数字   gcc   gdb   valgrind

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!