Some of my variables change values seemin开发者_开发百科gly 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).
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 or delete
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 via free
or delete
.
Have you tried using valgrind to check for memory errors in your code?
精彩评论