GNU GDB的malloc返回的指针无法访问 [英] gnu gdb malloc return inaccessible pointer
问题描述
在的malloc调试的gdb会话中运行一些codeS后返回人迹罕至的地址。
malloc in gdb debug session returns inaccessible address after running some codes.
在主函数开始第一次突破。
一切正常。
first break at the start of main function. everything is ok.
Breakpoint 9, main (argc=5, argv=0x7fffffffe418) at src/ose/sdv/ose_sdv/linux/main.c:557
557 char *cfgfile = NULL;
(gdb) call malloc(4)
$50 = 23293968
(gdb) x 23293968
0x1637010: 0x00000000
(gdb) c
运行它开始恢复无法访问的内存地址也就是开始于0xffffffff的一些行之后〜
after running some lines it begins to return inaccessible memory address which is start at 0xffffffff~
Program received signal SIGINT, Interrupt.0x00007ffff70c1f4d in read () from /lib64/libc.so.6
(gdb) call malloc(4)
$52 = -1811110576
(gdb) x -1811110576
0xffffffff940ca550: Cannot access memory at address 0xffffffff940ca550
我使用的是64位Linux操作系统。
i'm using a 64bit linux os.
我找不到code的确切行导致此。
i cannot find the exact line of code that causes this.
之后的malloc开始喜欢这行始终不变。
The line after which malloc begins to like this is always change.
在运行时没有异常发生,程序如预期运行。
我不知道什么是错在这里。
No exception occurred during runtime and the program runs as expected. I am not sure what is wrong here.
推荐答案
有是的的bug的您的节目。在C ++中的错误并不总是给予例外,和C没有例外的。阅读关于未定义行为。
There are bugs in your program. Bugs in C++ don't always give exceptions, and C has no notion of exceptions. Read about undefined behavior.
输入的调用malloc(4)
在 GDB
你问 GDB
调用的malloc
您的马车进程中。
When typing call malloc(4)
under gdb
you ask gdb
to call malloc
inside your buggy process.
之所以的malloc
(或新
)可以从一个运行到下给予不同的地址是<一个HREF =http://en.wikipedia.org/wiki/ASLR相对=nofollow> ASLR 。如果你想,你可以禁用ASLR通过
The reason why malloc
(or new
) may give different addresses from one run to the next is ASLR. You could disable ASLR if you wanted to by
echo 0 > /proc/sys/kernel/randomize_va_space
您应该的gcc -Wall -g
编译和使用 GDB
作为调试器(也许是看
GDB
可能是有用的)。
You should compile with gcc -Wall -g
and use gdb
as a debugger (perhaps the watch
command of gdb
could be useful).
何种情况可能会是你错误地改写一些字堆外的malloc
-ed存储区,或者有一个的缓冲区溢出,或使用未初始化变量,等等...
What might have happened is that you wrongly overwrote some word outside a heap malloc
-ed memory zone, or got a buffer overflow, or used an uninitialized variable, etc...
的一个好方法混乱的malloc
实在太差是前一段的malloc
-ed区喜欢写为int * p =的malloc(4); P [-1] = 1234;
那么未来免费
和的malloc
可以表现出非常怪异行为...
A good way to mess malloc
really badly is to write before some malloc
-ed zone like int *p = malloc(4); p[-1]=1234;
then future free
and malloc
could exhibit very weird behavior...
你应该使用的valgrind 追捕你的内存错误。尝试运行
And you should use valgrind to hunt your memory bugs. Try running
的valgrind yourprogram
的您的程序论点的....
通过最近的海湾合作委员会(即4.8),你也可以尝试编译和链接-fsanitize =地址选项(除了 -Wall -g
)
With a recent GCC (i.e 4.8) you could also try compiling and linking with -fsanitize=address option (in addition to -Wall -g
)
这篇关于GNU GDB的malloc返回的指针无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!