I had my share of issues with this one before.
You are overwriting "i" with 0 and depending of the compiler optimization this i could be the i next to the int array[10] in memory or some other local variable.
int array[10],i;
for (i = 0; i <=10 ; i++)
array[i]=0;
Should be
int array[10],i;
for (i = 0; i< 10 ; i++)
array[i]=0;
When you have mysterious error in C++ code that makes no sens then reverse the local variables:
int array[10],i;
to
int i, array[10];
or add a dummy
int int dummy[10], array[10], i;
Also realize that 2 different functions in 2 different source files could be finally compiled very close to each other in memory interfering with each other.
How do I avoid traps like this? By building a code base that I can rely on. When mysterious bugs occur then it is easier to track in what you changed before.
int array[10],i;
for (i = 0; i <=10 ; i++)
array[i]=0;
It will be a memory stomp if variable i is stored adjacent to array, since array[10] will use a piece of memory without realizing that it is still in use
5 comments
2 u/roznak 17 Feb 2016 15:18
I had my share of issues with this one before. You are overwriting "i" with 0 and depending of the compiler optimization this i could be the i next to the int array[10] in memory or some other local variable.
Should be
When you have mysterious error in C++ code that makes no sens then reverse the local variables:
int array[10],i;to
int i, array[10];or add a dummy
int int dummy[10], array[10], i;Also realize that 2 different functions in 2 different source files could be finally compiled very close to each other in memory interfering with each other.
How do I avoid traps like this? By building a code base that I can rely on. When mysterious bugs occur then it is easier to track in what you changed before.
2 u/amazner [OP] 17 Feb 2016 15:35
That was a very good explaination of how to avoid the issue (y)
2 u/SelfReferenceParadox 17 Feb 2016 17:31
Another fun one:
1 u/zak_the_mac 18 Feb 2016 00:53
I usually see this kind of error referred to as an 'array overrun' rather than the more generic 'memory stomp'.
0 u/amazner [OP] 18 Feb 2016 03:43
Yeah.. but in the case
It will be a memory stomp if variable i is stored adjacent to array, since array[10] will use a piece of memory without realizing that it is still in use