What Is "Memory Stomp"?

3    17 Feb 2016 04:02 by u/amazner

5 comments

2

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.

2

That was a very good explaination of how to avoid the issue (y)

2

Another fun one:

for(byte i = 0; i < 256; i++)
1

I usually see this kind of error referred to as an 'array overrun' rather than the more generic 'memory stomp'.

0

Yeah.. but in the case

  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