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?

ST3
8,9825 gold badges72 silver badges98 bronze badges
asked Dec 2, 2012 at 12:37
1

3 Answers 3

122

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.

answered Dec 2, 2012 at 12:43
Sign up to request clarification or add additional context in comments.

1 Comment

Here is nice example of memory stomping.
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.

einpoklum
138k86 gold badges448 silver badges923 bronze badges
answered Dec 2, 2012 at 12:42

Comments

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".

answered Jul 14, 2015 at 8:41

2 Comments

There is anther thread, discussing pretty much that same example on different operating system... stackoverflow.com/questions/31016660
@Christian It has nothing to do with an OS. This is an undefined behavior.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.