What is a “memory stomp”?

scravy picture scravy · Dec 2, 2012 · Viewed 14.8k times · Source

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?

Answer

David Schwartz picture David Schwartz · Dec 2, 2012

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.