Question:
Run time stack question in C! What happens if the run time stack and heap allocated so much, they overlap?
anonymous
2012-04-25 23:22:20 UTC
Heap allocates stuff in memory and goes down, run time stack allocates in memory and goes up the stack. What happens if they overlap?
Six answers:
Unca Alby
2012-04-26 00:04:18 UTC
The Operating System and the various library functions (malloc, etc.) control the run-time stack and the heap. That software is set up to detect and prevent overlap. Or it may allow some overlap, assuming you won't be maxing out the stack and the heap at the same time. But it will keep a look-out for the sizes and locations of everything, including what may have been swapped out to disk. That's part of any well designed OS.



If you max out both at the same time, what will happen is, when you make one recursive call too many, causing the stack to overflow, your application will be crashed by the OS. On Linux, you'll get something like a Segment Violation core-dump. Or, when you make one too many "malloc" calls, attempting to get more memory than what is currently available, "malloc" will return a NULL pointer.



If your code is written correctly, it won't have too many recursive calls that might overflow the stack. In normal operations, the run-time stack doesn't need to be very large, so if you blow that stack, there's probably a bug in your code.



Also if your code is written correctly, it will test for that NULL pointer return from malloc and do something appropriate (like logging an error message somewhere then halting). If it doesn't test, it will just keep on running until it attempts to use that NULL pointer, and then the OS will crash your application with a Memory Access Violation core-dump.
Catherine
2016-02-24 01:36:31 UTC
You should use the stack for small data items that have a lifetime that aligns with the scope their declared in. You should use heap for large objects whose lifetime is independent of the scope their created in. I would note that dynamic allocation is available both on the heap through functions like malloc and on the stack through functions like _alloca. You would use that for small buffers whose size you don't know at compile time. Also available is static memory, non-stack memory allocated at compile time. If you've got something you know you only need one of and needs a lifetime similar to the lifetime of the application then static is a good choice.
Jim
2012-04-25 23:57:41 UTC
there are protections in place to prevent that in the way the heap is used for memory management. I am almost sure you will get a failure to allocate. or compile. or run.
James Bond
2012-04-25 23:37:23 UTC
A function call my damage or spoil a dynamic array.

If a call to a dynamic array creation spoils an activation record, especially return address where the control goes!!!
green meklar
2012-04-26 10:09:54 UTC
They can't. The computer has guards against that and will forcefully terminate the program if it tries to allocate too much of either heap or stack memory.



You can try this by creating a recursive function that calls itself forever. Run it, and within a few seconds the program will crash, possibly saying something like 'out of stack space'.
John H
2012-04-26 02:11:42 UTC
A typical modern OS will not allow the stack to grow to the point where it runs into the heap. Since the stack automatically grows downward as functions are called to a greater and greater call depth, there isn't unmapped memory beyond the end of the current stack (like there is beyond the heap). Instead, an unmapped "guard page" is maintained just beyond the limit that the stack is allowed to grow to, so that if the stack ever grows to that point, any memory access to the stack will attempt to access unmapped memory and result in an access violation (Windows-speak) or segmentation violation (UNIX/Linux-speak).



If your stack and heap grew to the point where they were not far from each other, and you had a function that declared a large array on the stack that "spanned" the gap between them (including the guard page), it might be possible to get them to run into each other without your program immediately crashing. But as soon as you accessed memory in the range of the guard page, your program would get an access violation.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...