user2979503
user2979503

Reputation: 13

How to print the current stack usage in c with printf?

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.

Upvotes: 0

Views: 1539

Answers (2)

unwind
unwind

Reputation: 399813

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.

Upvotes: 5

ensc
ensc

Reputation: 6984

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

Upvotes: 0

Related Questions