What happens when a stack and heap collide

I am curious to know what happens when the stack and heap collide. If anyone comes across this, please explain the scenario.

Thanks in advance.

+45
c ++ c stack heap memory
Aug 26 '09 at 11:26
source share
5 answers

In modern languages, running on a modern OS, you will get either a stack overflow (cheers!), Or malloc() or sbrk() or mmap() if you try to increase the heap. But not all software is up-to-date, so let's look at failure modes:

  • If the stack turns into a heap, a typical C compiler will silently overwrite the heap data structures. On a modern OS, one or more protected pages will exist that prevent unlimited stack growth. As long as the amount of memory on the protection pages is not less than the size of the growing record of the activation procedure, the OS guarantees you segfault. If you use DOS on a machine without an MMU, you will probably be closed.

  • If the heap grows onto the stack, the operating system should always be aware of this situation, and some kind of system call will fail. The implementation of malloc() almost certainly notices an error and returns NULL . What happens after that is up to you.

I am always surprised at the desire of the compiler authors to hope that the OS will put the protective pages in place to prevent stack overflows. Of course, this trick works well until you start having thousands of threads, each with its own stack ...

+54
Aug 26 '09 at 15:03
source share

It depends on the platform. On many platforms, this actually cannot be at all (the heap and stack are allocated on different pages and will not occur twice.

Keep in mind that the idea of ​​growing a heap up and a stack growing down is only conceptual. On very small systems (for example, on old 8-bit microshes that ran CP / M), as well as on some POS and other systems with flat memory (without MMU or any other virtual or protected memory), the heap and stack could actually be that way. In this case, the behavior will be undefined ... but it will almost certainly fail as soon as the code tries to return to some address at the top of the damaged stack or follows an indirect pointer from one part of the heap to another or ...

In any case, you will not see it on any modern general-purpose workstation or on the server. You press the resource limit and get malfunctions in malloc, or you run into virtual memory, and ultimately the system will break into a trembling heap of β€œclick on the red switch”.

+42
Aug 26 '09 at 11:36
source share

In times like these, it takes time to turn to the wise words of Dr. Egon Spengler ....

  • Dr. Egon Spengler: There is something very important, I forgot to tell you.
  • Dr. Peter Wenkman: What?
  • Dr. Egon Spengler: Do not let the heap collide with the stack.
  • Dr. Peter Wenkman: Why?
  • Dr. Egon Spengler: It would be bad.
  • Dr. Peter Wenkman: I'm a little vague on the whole good / bad. What does "bad" mean?
  • Dr. Egon Spengler: Try to imagine life as you know that it stops instantly, and every molecule in your body explodes at the speed of light.
  • Dr. Ray Stantz: Complete proton reproduction!
  • Dr. Peter Wenkman: This is bad. Good. Ok, important safety tip. Thanks, Egon.
+11
Aug 26 '09 at 15:09
source share

You get a memory exception or a stack exception if you're lucky. If you're out of luck, the program goes into invalid memory and either throws the wrong memory exception. If you are very unlucky, the program continues and destroys what it should not, and you never know why your program failed.

Finally, of course, the universe can crack.

+4
Aug 26 '09 at 11:30 a.m.
source share

If a stack / heap overflow occurs, you will get a segmentation error or memory allocation. Here is an example:

 void recursiveFun () { static int i; // char *str= (char *)malloc (100); printf ("%d\t", i++); recursiveFun (); // free (str); } 

Suppose you call the function above, it ends in the stack, and the program crashes. Now delete the commented lines and call the function again, you will find that the segmentation error occurs in less time and less recursion than the previous one. [In my test environment, the stack overflow occurred after recursion 5237765 in the first case, whereas in the second scenario, it happened after recursion 2616325.]

+1
Jun 28 '15 at 10:52
source share



All Articles