Will the stack of a C program ever shrink?

前端 未结 3 1949
说谎
说谎 2021-01-19 14:04

I\'ve noticed that every running C program has a private mapping called [stack] that is initially quite small (128k on my machine), but will grow to accomodate any automatic

3条回答
  •  执念已碎
    2021-01-19 14:12

    I believe that the Linux kernel is growing the stack segment (only for the main thread). It is not in the compiler (except by incrementing the stack pointer at calls, and ignoring the experimental -fsplit-stack option of recent GCC), and not in the libC.

    If you are sure your stack has grown too big, and you won't need it, you might perhaps munmap the unused part (but be careful; kernel developers don't think of this so it might not work as expected; in the early 1990-s I remembered having crashed SunOS5.0 on Sparc with such dirty tricks).

    And on Linux, x86-64, with a decent machine, you really should not care. The stack is not that big...

    My guess is that the stack segment is mmap-ed with MAP_NORESERVE MAP_STACK MAP_GROWSDOWN but I may be wrong.

提交回复
热议问题