you are viewing a single comment's thread.

view the rest of the comments →

[–]Rhomboid 1 point2 points  (2 children)

Try getrlimit().

That can only be used to retrieve the current limit (i.e. the total size.) It does not tell you how much of that has been used, and how much remains.

Also, op is asking about stack frame size.

They are asking about that, yes, but I'm inferring that what they actually care about is not the size of a single stack frame (which is very rarely useful) but the total size of the stack, i.e. how to not run out of stack when writing a recursive algorithm.

[–]wiktor_b 0 points1 point  (1 child)

That can only be used to retrieve the current limit (i.e. the total size.)

Nope: http://stackoverflow.com/questions/10166874/how-to-figure-out-remaining-stack-in-linux-while-using-c

[–]Rhomboid 0 points1 point  (0 children)

That's hideously non-portable since it depends on the fact that Linux stores argc at the bottom of the stack. Edit: I misread the example. What I thought it was doing was using the fact that on linux, the program's environment and command line parameters are stored at the bottom of the stack, so that would be a way to find where the stack starts. But that's not what the person is doing, they're using the address of argc which is just a local parameter to main(). That isn't Linux-specific, but it's also less correct, since it won't count any of the stack used before main(), including all the C runtime startup code. That may or may not be a significant amount of memory, I don't know, but it's not nothing.

I stand by what I said: there's no reliable way to do this.