This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

How to get current stack size ?


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



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]