C语言 当所有变量都被使用时,不是 stack'd、malloc'd 或(最近)free'd
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23791398/
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
Is not stack'd, malloc'd or (recently) free'd, when all the variables is used
提问by I159
When I call this function:
当我调用这个函数时:
int within(struct key *node, int *value) {
int length = len(node);
if ((value <= node[length-1].data) && (value >= node[0].data)) // Problematic line
return 0;
else if (value > node[length-1].data)
return 1;
else if (value < node[0].data)
return -1;
}
Valgrind raises this error message:
Valgrind 引发此错误消息:
Address 0x51f60a0 is not stack'd, malloc'd or (recently) free'd
地址 0x51f60a0 未被堆栈、malloc 或(最近)释放
valueand nodevariables have correct values. All the variables is used. What is wrong with memory management there? Please, tell me, if the snippet doesn't clear the situation.
value和node变量有正确的值。使用了所有变量。那里的内存管理有什么问题?请告诉我,如果代码片段没有说明情况。
回答by oliver
The message Address 0x51f60a0 is not stack'd, malloc'd or (recently) freeis usually only a part of a larger Valgrind error message.
该消息Address 0x51f60a0 is not stack'd, malloc'd or (recently) free通常只是较大的 Valgrind 错误消息的一部分。
These Valgrind error messages usually looks something like this:
这些 Valgrind 错误消息通常如下所示:
Invalid read of size 4
at 0x40F6BBCC: (within /usr/lib/libpng.so.2.1.0.9)
by 0x40F6B804: (within /usr/lib/libpng.so.2.1.0.9)
by 0x40B07FF4: read_png_image__FP8QImageIO (kernel/qpngio.cpp:326)
by 0x40AC751B: QImageIO::read() (kernel/qimage.cpp:3621)
Address 0xBFFFF0E0 is not stack'd, malloc'd or free'd
or
或者
Invalid read of size 8
at 0x40060E: free_adj_list (main.c:9)
by 0x400844: main (main.c:65)
Address 0x4c1d170 is 16 bytes inside a block of size 24 free'd
at 0x4A04D72: free (vg_replace_malloc.c:325)
by 0x400609: free_adj_list (main.c:8)
by 0x400844: main (main.c:65)
How to read these error messages
如何阅读这些错误信息
The first part of the message indicates what went wrong ("Invalid read of size 4" would mean that you tried to read from a memory address which you should not access), followed by the backtrace where the error occurred.
消息的第一部分指示出了什么问题(“大小为 4 的无效读取”意味着您试图从不应访问的内存地址读取),然后是发生错误的回溯。
The backtrace is followed by details about the memory address which you tried to access. Valgrind makes a guess here as to what you might have meant, by looking whether the address is:
回溯之后是有关您尝试访问的内存地址的详细信息。Valgrind 通过查看地址是否为:
- just outside of a a part of memory that you do have access to (so your program did a buffer overrun). Example message would be
Address 0x1002772ac is 4 bytes after a block of size 12 alloc'd - inside a block of memory that was free'd before (so your program used memory after it was freed); example:
Address 0x4c1d170 is 16 bytes inside a block of size 24 free'd
- 就在您有权访问的内存部分之外(因此您的程序发生了缓冲区溢出)。示例消息是
Address 0x1002772ac is 4 bytes after a block of size 12 alloc'd - 在之前被释放的内存块中(所以你的程序在它被释放后使用了内存);例子:
Address 0x4c1d170 is 16 bytes inside a block of size 24 free'd
And these messages are then followed by a second backtrace which indicates where you allocated or freed the memory mentioned.
然后这些消息之后是第二个回溯,指示您在哪里分配或释放提到的内存。
But the message Address 0x51f60a0 is not stack'd, malloc'd or (recently) free'dmeans that Valgrind couldn't guess what you meant to do. You tried to access memory at 0x51f60a0 but that address was not recently freed, and is not near any other part of memory you have allocated. So you can be reasonably sure that the error in this case is neither a buffer overrun nor is it a use-after-free error.
但是该消息Address 0x51f60a0 is not stack'd, malloc'd or (recently) free'd意味着 Valgrind 无法猜测您的意图。您试图访问 0x51f60a0 处的内存,但该地址最近没有被释放,并且不在您分配的任何其他内存部分附近。因此,您可以合理地确定这种情况下的错误既不是缓冲区溢出,也不是释放后使用错误。
How to debug errors like this
如何调试这样的错误
So we can assume that 0x51f60a0 is some more or less "random" memory address. I can think of mainly two possible causes for this:
所以我们可以假设 0x51f60a0 或多或少是一些“随机”内存地址。我能想到的主要有两个可能的原因:
- the pointer you dereferenced contained some uninitialized value; in this case you should also get a
Use of uninitialised valueerror message from Valgrind - you dereferenced a value that was not intended as a pointer at all - eg. the value might actually be the result of some unrelated calculation in your program, and somehow you wrote that value to the pointer that you used later
- 您取消引用的指针包含一些未初始化的值;在这种情况下,您还应该
Use of uninitialised value从 Valgrind收到错误消息 - 您取消引用了一个根本不打算用作指针的值 - 例如。该值实际上可能是程序中某些不相关计算的结果,并且以某种方式将该值写入了稍后使用的指针
Apart from these, there is of course still the possibility that the error is in fact some buffer overrun or use-after-free but Valgrind failed to detect it.
除此之外,当然还有可能错误实际上是某些缓冲区溢出或释放后使用,但 Valgrind 未能检测到它。
How to debug this error in your program
如何在程序中调试此错误
I think one way to narrow down the problem would be to start the application in Valgrind with GDBto find out what memory access exactly causes the error (is nodebad? Is node[length-1]bad? Is node[0]bad?). Then find out how the bad value came there in the first place.
我认为缩小问题范围的一种方法是使用 GDB在 Valgrind 中启动应用程序,以找出导致错误的确切内存访问(node不好?node[length-1]不好?node[0]不好?)。然后找出错误的价值是如何首先出现的。
回答by KevinDTimm
Your comparison(s) should be *value <= node[length-1].data
notvalue <= node[length-1].data
您的比较*value <= node[length-1].data
不应该value <= node[length-1].data
IOW, you're missing the asterisk before the valuevariable.
IOW,您在value变量之前缺少星号。
回答by duleshi
What caused my problem of "not stack'd, malloc'd or (recently) free'd". I hope this is useful to someone directed here by a search engine like me.
是什么导致了我的“not stack'd, malloc'd or (recently) free'd”问题。我希望这对像我这样的搜索引擎指向这里的人有用。
In my case, I allocated a heap array pof size 585. But then I tried to access pin the index range of 733~1300. And valgrind just showed that message.
在我的例子中,我分配了一个p大小为 585的堆数组。但后来我尝试p在 733~1300 的索引范围内访问。valgrind 刚刚显示了该消息。

