您可以通过查找进程堆栈空间的大小然后减去使用的数量来确定进程可用的堆栈空间.
ulimit -s
显示了linux系统上的堆栈大小.对于程序化方法,请查看getrlimit().然后,要确定当前堆栈深度,请从一个到底部减去指向堆栈顶部的指针.例如(代码未经测试):
unsigned char *bottom_of_stack_ptr; void call_function(int argc, char *argv) { unsigned char top_of_stack; unsigned int depth = (&top_of_stack > bottom_of_stack_ptr) ? &top_of_stack-bottom_of_stack_ptr : bottom_of_stack_ptr-&top_of_stack; if( depth+100 < PROGRAMMATICALLY_DETERMINED_STACK_SIZE ) { ... } } int main(int argc, char *argv) { unsigned char bottom_of_stack; bottom_of_stack_ptr = &bottom_of_stack; my_function(); return 0; }