Stack overflow, in short, is the stack is full, but also to plug things inside.
one, Stack overflow
The main causes of stack overflow are the following aspects 1.1 recursive hierarchy too deep
Because of too many function calls, the call stack cannot accommodate the return addresses of these calls, which are typically generated in recursion. The stack overflow is likely to be generated by infinite recursion (Infinite recursion), but it may be just an excessive stack hierarchy.
The following section of code:
void foo () {
int A;
Foo ();
}
int main ()
{
foo ();
return 0;
}
The results of the operation are as follows:
The first two concepts are explained:
ESP: The stack pointer, which points to the top of the stack frame (the Next top of the active record that presses onto the stack), and EBP is the frame pointer to the bottom of the current active record. Stack frame can understand the entire stack of the sub-stack, the program stack by child stack composition. The default size for stacks in Windows is 2MB.
For the above two results graphs, according to the EBP pointer in the reduction can be seen, each into a recursive, will add a stack frame (sub stack). and the program stack space is limited, when the iteration level is too deep, will cause the stack is not enough, at this time if continue recursion, will cause overflow.
1.2 Local variables for large data structures
Local variables are stored in the stack area, and if the sizeof of local variables is too large, the stack overflow is also caused. But this situation is difficult to meet in practice.
Ii. Transboundary
In a 32bit system, the char type occupies 1 bytes, where the highest bit of signed char represents the symbol, that is, only 7bit represents the data, and unsigned char is all bits representing the data. Therefore, the signed Char representation range is -128 ~ 127, and unsigned char's representation range is 0 ~ 255.
So for the following code, what is the value of the C1?
Signed char a = 128;
Std::bitset<sizeof (char) *8> C (a); Obtains A's binary notation
cout << c << Endl;
The results are as follows:
That is, 128 (negative numbers are represented in the computer by their complement: the inverse code + 1).
If you continue with the addition and subtraction operation, what problems will occur ... In fact, the normal addition and subtraction, such as 128 of the binary is 1000 0000, then add 1 above the base into 1000 0001 (that is, 127), then minus 1. is 0111 1111. As follows:
Signed char a = 128; -128
signed Char d = A-(char) 1; 127
But for 1111 1111 (-1), if you continue +1 at this point, then it becomes 1 0000 0000, and because char is 8 bit, for this case, the highest bit goes, and the final result is 0000 0000.