Detecting stack overflows during runtime beforehand
Heres a simple solution that works for win-32. Actually resembles what Wossname already posted but less icky :)
unsigned int get_stack_address( void )
{
unsigned int r = 0;
__asm mov dword ptr [r], esp;
return r;
}
void rec( int x, const unsigned int begin_address )
{
// here just put 100 000 bytes of memory
if ( begin_address - get_stack_address() > 100000 )
{
//std::cout << "Recursion level " << x << " stack too high" << std::endl;
return;
}
rec( x + 1, begin_address );
}
int main( void )
{
int x = 0;
rec(x,get_stack_address());
}
Finding potential stack overflow issues in templates
One approach is to use static_assert
on the size of the element inside the template code, like this:
static_assert(sizeof(TYPE) < 16384, "Object is too large for the stack");
TYPE Temp = newElement;
This would break a compile in every place where the template is instantiated with a type that is too large for the stack.
Related Topics
Getting the Current Time (In Milliseconds) from the System Clock in Windows
Member Initializer Does Not Name a Non-Static Data Member or Base Class
There Are No Arguments That Depend on a Template Parameter
Where How to Use List Initialization
Which MACro to Wrap MAC Os X Specific Code in C/C++
Proper Way of Casting Pointer Types
Can Templates Be Used to Access Struct Variables by Name
Testing If Given Number Is Integer
C++ Cannot Convert from Base a to Derived Type B via Virtual Base A
Best Way to Start a Thread as a Member of a C++ Class
What's the Difference Between New Char[10] and New Char(10)
Can #If Pre-Processor Directives Be Nested in C++
How to Use Cmake_Export_Compile_Commands