tags:

views:

875

answers:

4

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!

A: 

There is no way of doing this portably - the C++ Standard does not actually require a stack.

anon
Well now I am very confused. I was under the impression that the stack was an essential area of C++ memory. Could you please explain how a C++ program operates without a stack? Thank you.
Phage
The C++ standard does not place any burden on the implementation. It only talks about an allocated store and a free store. You'd do a new to get stuff off of free store, fallback otherwise on the allocated store -- you get the drift.
dirkgently
The compiler could use a linked list of call frames rather than a block of memory indexed by a CPU register (aka the stack). All implementations I'm aware of use the latter, but the C++ standard does not require it.
anon
Alright, so regardless of implementation, there is not a way to find out the limit (if any) of the allocated store inside your C++ program?
Phage
You can't disregard the implementation - your specific implementation may provide some way of doing it, but that method may not (almost cerainly will not) work on another implementation.
anon
Perhaps the confusion is in the *wording* of your answer? It's not that "a stack isn't required" per se but that things like "stack size" or stack location or call-stack implementation is not specified. Fact is, real implementations do of course use a stack!
Jason Cohen
As I said in my first comment here.
anon
+3  A: 

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

antti.huima
+4  A: 

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.

dirkgently
If I could upvote you I would. This helped tremendously, thank you for explaining.
Phage
Don't worry about upvoting. Happy to help. Let us know if you have further questions.
dirkgently
The part of the answer about --stack= is completely wrong: here is what 'info ld' says about it: This option is specific to the i386 PE targeted port of the linker. On UNIX systems, the compiler typically has *nothing* to do with stack limit.
Employed Russian
I don't think I mentioned it to be a gold-standard or that they work on all Unix systems. I took a few examples, which the reader is supposed to investigate.
dirkgently
+2  A: 

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.

Jason Cohen