简体   繁体   中英

How can you find out the maximum size of the memory stack for a C++ program on linux? (gnu compiler)

I am curious about how to find out what the maximum stack size is for a particular compiler/os combo. I am using Ubuntu/GNU compiler. A few questions I have in addition are:

  1. Who controls the default maximum stack size; OS or compiler?
  2. Is the default maximum scaled according to total memory? (ie a machine with 2gb memory would have larger default size than a machine with only 512mb) For this example both machines are same os/compiler setup, just different amounts of system RAM.

Thanks!

Who controls the default maximum stack size; OS or compiler?

The compiler typically. The OS/hardware does limit it to a certain extent. Default is 8MB on linux IIRC. Think of ulimit -s on Linux (to change stack sizes).

Is the default maximum scaled according to total memory? (ie a machine with 2gb memory would have larger default size than a machine with only 512mb) For this example both machines are same os/compiler setup, just different amounts of system RAM.

No. Until and unless you do it yiurself.You can alter stack sizes via compiler switches.

ld --stack=<STACK_SIZE>

or

gcc -Wl,--stack=<STACK_SIZE>

The C++ Standard's take on the issue of stacks and heaps:

The standard is based on an abstract machine and does not really concern itself with hardware or stacks or heaps. It does talk about an allocated store and a free store. The free store is where you'd be if you are calling new (mostly). FWIW, an implementation can have only one memory area masquerading as both stack and heap when it comes to object allocation.

Your question, therefor, boils down to be an implementation specific issue rather than a language issue.

Hope this helps.

On Linux (Ubuntu), operating system controls the maximum size. See "man limit" or "man ulimit" for reference.

Nowadays, the correct question is: How much memory is allocated to my thread. Each thread gets an amount which typically you can control at thread-creation time.

To answer part 1, the compiler / thread-system gets to pick, although some OS's have (historically) had limits.

For 2, no it's not scaled.

没有办法这样做 - C ++标准实际上并不需要堆栈。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM