QNX RTOS v4 Knowledge Base
QNX RTOS v4 Knowledge Base
Title |
Determining how much stack my program requires |
Ref. No. |
QNX.000009308 |
Category(ies) |
Development |
Issue |
I have a program that includes several recursive functions. When I run this program, using the default stack size, the application stack will overflow. But if I bump the stack size up to 50K, everything runs fine. Problem is, raising the stack size that much, really bloats my application.
Is there a way I can accurately determine how much stack my program requires?
|
Solution |
Here's a sample program that can find how much stack is used by recursive functions. By incorporating a similar approach into your application, you can determine the worst-case stack requirements for specific areas of code.
#include <string.h> #include <stdio.h> #include <stdlib.h>
extern int _STACKLOW, _STACKTOP;x09/*x09magic */ long recursive = 0;
x09/* used to fill the currently unused portion of the stack with filler chars */
stack_fill(char filler) { x09char *stack_bottom = (char near *) _STACKLOW;
x09memset(stack_bottom, filler, (short)&stack_bottom-_STACKLOW-sizeof stack_bottom);
x09/* used to count # of filler chars left on stack starting from bottom counting upwards */
stack_check(char filler){ x09char *stack_bottom = (char near *) _STACKLOW; x09long i;
x09for (i=0; stack_bottom[i] = filler ;i++); x09printf("Stack left = %ld bytesn", i);
} x09x09/* used to overwrite filler chars on stack up to 'count' times */
recurse(int count{ x09recursive++; x09if (recursive <count) x09recurse(count);
}
main(int argc, char *argv[]) {
x09if (argc < 2) exit(-1); x09stack_fill('*'); x09recurse(atoi(argv[1])); x09stack_check('*');
}
|
|