100

I just came across this blog post which mentions “stomping memory”:

a C++ program which is easily capable of stomping memory (something you probably have never even heard of if you were born in a managed code world.)

And in fact I have never heard of it!

So, what is this, a memory stomp, stomping memory? When does it occur?

1

3 Answers 3

121

Memory is "stomped" when a piece of code manipulates memory without realizing that another piece of code is using that memory in a way that conflicts. There are several common ways memory can be stomped.

One is allocating, say, 100 bytes of memory but then storing something past the 100th address. This memory might be used to hold something completely different. This is particularly hard to debug because the problem will appear when something tries to access the victim that was stomped on, and the code that stomped on it may be totally unrelated.

Another is accessing memory after it was freed. The memory may be allocated for another object. Again, the code that shows the problem may be related to the newly-allocated object that got the same address and unrelated to the code that caused the problem.

1
  • 3
    Here is nice example of memory stomping. Jul 22, 2015 at 21:42
34

Very often it is a buffer overrun; as an example, this code:

char buffer[8];
buffer[8] = 'a';

will "stomp" on whatever happens to be in the next thing in memory after buffer. Generally speaking, 'stomping' is when memory is written to unintentionally.

10

Other answers basically are correct, but I would like to give an example.

int a[10], i;       
for (i = 0; i < 11 ; i++)
    a[i] = 0;

int i, a[10];     
for (i = 0; i < 11 ; i++)
    a[i] = 0;

These samples may lead into infinite loop (or may not lead), because it is undefined behavior.

Very likely variable i in memory is stored just after array. So accessing a[10] could actually access i in other words it could reset loop counter.

I think it is good example that demonstrates memory "stomping".

2
  • 1
    There is anther thread, discussing pretty much that same example on different operating system... stackoverflow.com/questions/31016660
    – Christian
    Jul 21, 2015 at 22:45
  • 2
    @Christian It has nothing to do with an OS. This is an undefined behavior.
    – ST3
    Jul 22, 2015 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.