How to get current stack size ?

Igor Bukanov igor@icesoft.no
Thu Feb 20 02:05:00 GMT 2003


Hi!

Is there any internal function in GCC that I can use to determine 
currently consumed stack space? Or I should better ask glibc guys about it?

I would like to prevent too deep recursion in a recursive descent parser 
which parse external data and limiting amount of stack space that can be 
consumed during recursion seems to be a simple solution. Initially I 
thought to use something like the following scheme which calculates 
address differences:

char* stack_mark;

void parse() {
	char dummy;
	stack_mark = &dummy;
	...
	recursively_called_function();
	
	....

}

void recursively_called_function() {
	char dummy;
	ptrdiff_t consumed_stack_size = &dummy - stack_mark;
	if (consumed_stack_size > STACK_LIMIT
	    || -consumed_stack_size > STACK_LIMIT)
	{
		report_error();
	}
	...
}

The trouble is that parse() can be called from some other libraries that 
may also be under deep recursion and checking against stack size 
consumed during parse may miss stack overflow. So I need absolute value 
for the stack size.

Thanks in advance, Igor Bukanov




More information about the Gcc mailing list