Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the stack overflow?

Tags:

stack

memory

Ok. so my understanding of how executables are laid out in memory is... image a square box that represents the memory accessible by your app.

The program code resides at the bottom of the memory, the stack is allocated to a spot just beyond the program code and is allocated upwards. the heap starts at the top of the memory and is allocated downwards.

If this is the case, why is it possible to allocate more heap memory than stack memory?

like image 260
mfc Avatar asked Mar 06 '26 19:03

mfc


1 Answers

Because even on modern systems with lots of virtual memory available, the maximum size of the call stack is usually deliberately limited to, say, 1MB.

This is not usually a fundamental limit; it's possible to modify this (using e.g. setrlimit() in Linux, or the -Xss flag for Java). But needing to do so usually indicates an abnormal program; if you have large data-sets, they should normally be stored on the heap.

like image 97
Oliver Charlesworth Avatar answered Mar 10 '26 10:03

Oliver Charlesworth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!