0

Basically the title says it all. I need to print, how much of the stack is currently used by the running program and I need to print it with printf().

EDIT: I have to overflow the stack. In order to do that, I wrote a recursive function, which indefinitely calls itself. I have to print the stack usage repeatedly, to show, how the stack becomes smaller.

EDIT2: The platform is gcc on linux.

3
  • 3
    This is platform dependent... Commented Nov 18, 2013 at 16:17
  • You could just keep allocating a huge buffer on the stack say a 10 KB buffer or so and keep an incremental count until you hit the issue. For linux, you could also refer to this question: stackoverflow.com/questions/6270945/linux-stack-sizes Commented Nov 18, 2013 at 16:28
  • 1
    Print the address of a local variable, say a large buffer like Ganesh suggests. Commented Nov 18, 2013 at 16:37

2 Answers 2

5

There is no portable way of doing that, there is no guarantee from C that your program is using "a stack", even.

You must be more specific, perhaps there is an OS-specific approach you can use.

Edit: In Linux, you can use the getrlimit() function to query the OS for some parameters. You can get the max allowed stack size, which sounds like it would be very useful in order to grow the stack to that level. I'm not sure if you can get the current stack size, though.

Sign up to request clarification or add additional context in comments.

Comments

0

/proc/self/stat (see man 5 proc for details) and/or /proc/self/status (human readable) show current stack usage.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.