CYGWIN:内存中的变量自发改变值
当我更改 C++ 代码中的其他变量时,我的一些变量的值似乎不合理地更改。我已经使用 gdb 来硬件监视有问题的内存,即使我只是 cout << ,它也会发生。 “布拉”;
如果有人可以提供任何有关如何解决此问题的建议,我们将不胜感激!我被难住了!
当我运行 cygwin 文档中提供的示例最大内存程序时,我被告知我有 1.5 GB 的可用 RAM(这对于该程序的范围来说应该足够了)。
Some of my variables change values seemingly unjustifiably when I change other variables in my c++ code. I've used gdb to hardware watch the memory in question and it will occur even when I just cout << "bla";
If anybody could offer ANY suggestions as how to fix this, it would be greatly appreciated! I'm stumped!
When I run the sample max memory program provided in the cygwin docs, I am told I have 1.5 GB of available RAM (which should easily be enough for the scope of this program).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
变量“自发”更改值的最可能原因是缓冲区溢出。下一个最可能的原因是堆损坏(或在释放或删除内存(又名悬空内存)后使用内存)。
您没有说明更改的变量是全局变量、堆分配变量还是本地变量。我猜测堆已分配,在这种情况下,悬挂理论是最有可能的一种 - 任何可以合法分配内存的东西(例如
cout
)都可以获取并修改您通过free释放的内存
或删除
。The most likely cause of a variable "spontaneously" changing value is a buffer overflow. The next most likely cause is heap corruption (or using memory after it has been
free()
d ordelete
d (aka dangling memory)).You didn't say whether the variables that change are global, heap allocated, or local. I am guessing heap allocated, in which case the dangling theory is the most likely one -- anything that can allocate memory legitimately (e.g.
cout
) could grab and modify the memory you have released viafree
ordelete
.您是否尝试过使用 valgrind 检查代码中的内存错误?
Have you tried using valgrind to check for memory errors in your code?