Why stack is faster than heap and what exactly is stack?

Btw, on Apple M1, here is the behavior of ulimit:

$ ulimit -a
-t: cpu time (seconds)              unlimited
-f: file size (blocks)              unlimited
-d: data seg size (kbytes)          unlimited
-s: stack size (kbytes)             8176
-c: core file size (blocks)         0
-v: address space (kbytes)          unlimited
-l: locked-in-memory size (kbytes)  unlimited
-u: processes                       2666
-n: file descriptors                2560
$ ulimit -s unlimited
$ ulimit -a          
-t: cpu time (seconds)              unlimited
-f: file size (blocks)              unlimited
-d: data seg size (kbytes)          unlimited
-s: stack size (kbytes)             65520
-c: core file size (blocks)         0
-v: address space (kbytes)          unlimited
-l: locked-in-memory size (kbytes)  unlimited
-u: processes                       2666
-n: file descriptors                2560
$ ulimit -s 70000    
ulimit: value exceeds hard limit

So it seems the maximum stack size that is allowed is 65520 KB ~ 64 MB.

Consequently it would seem that one cannot have larger arrays allocated on stack, but I have not tested it.

2 Likes