C++ 地址错误时无法访问内存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5519824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot access memory at address error
提问by Snowman
I'm getting this error:
我收到此错误:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000407265 in Quadtree::deeper (this=0x7fffffffe430,
orig=@0x7fffffffe430, n=@0x7a1da0, tol=Cannot access memory at address 0x7fffff3feffc
) at quadtree.cpp:47
47 int Quadtree::deeper(QuadtreeNode * & orig, QuadtreeNode * & n, int tol, int tolNum) {
This is line 47:
这是第 47 行:
int Quadtree::deeper(QuadtreeNode * & orig, QuadtreeNode * & n, int tol, int tolNum) {
Whats weird is that I am not getting any valgrind errors at all, but only gdb error and seg fault at run time. What can this error possibly mean in a general sense (without having to see the rest of my code)?
奇怪的是,我根本没有收到任何 valgrind 错误,而在运行时只有 gdb 错误和 seg 错误。这个错误在一般意义上可能意味着什么(无需查看我的其余代码)?
回答by Josh Haberman
My best guess: you are seeing a stack overflow (What a coincidence, given the site we're on! :). I can't explain why Valgrind isn't catching it though: usually Valgrind uses the same stack size as the OS (at least on my system).
我最好的猜测:您看到堆栈溢出(真是巧合,考虑到我们所在的站点!:)。我无法解释为什么 Valgrind 没有捕获它:通常 Valgrind 使用与操作系统相同的堆栈大小(至少在我的系统上)。
What this error means is that your code tried to access memory at address 0x7fffff3feffc
-- either a read or a write, but that address is not currently memory-mapped into your address space. The instruction that performed this illegal read or write was at memory address 0x0000000000407265
.
这个错误意味着你的代码试图访问地址处的内存0x7fffff3feffc
——无论是读还是写,但该地址当前没有被内存映射到你的地址空间。执行此非法读取或写入的指令位于内存地址0x0000000000407265
。
If the compiler gives you the line number of your function's opening brace as the offending line, it might be in the function's prologue (the part that saves registers to the stack). That's why I suspect you have a stack overflow.
如果编译器为您提供函数左大括号的行号作为违规行,则它可能在函数的序言中(将寄存器保存到堆栈的部分)。这就是为什么我怀疑你有堆栈溢出。
On Linux, you can look at /proc/YOUR-PID/maps
to get a memory-map for the entire process. It will show you where the stack and heap are stored, as well as where the libraries are loaded. You could use this information to figure out what part of memory you've likely overflowed. Since the stack is usually (on Linux) placed at the very top of memory, you'll probably find that this very large address is very near to your stack.
在 Linux 上,您可以查看/proc/YOUR-PID/maps
以获取整个进程的内存映射。它将显示堆栈和堆的存储位置,以及库的加载位置。您可以使用此信息来确定可能溢出的内存部分。由于堆栈通常(在 Linux 上)位于内存的最顶部,您可能会发现这个非常大的地址非常靠近您的堆栈。
Good luck!
祝你好运!
回答by Ilya Kogan
Check that neither orig
, nor n
, nor the object of type Quadtree
you're calling deeper
on, have been freed (deleted).
检查既没有orig
,也没有n
,也没有类型的对象Quadtree
你打电话deeper
时,已被释放(删除)。