Stack overflow usually occurs due to unbridled recursion (although this can be caused in the normal course of events if you do not have enough stack space for the normal level of function calls, for example, with embedded systems or even with limited recursion if the limits are too large). Example below:
void f (void) {
f();
}
int main (void) {
f();
return 0;
}
In this example, the function f()calls itself very stupidly, and each time it does, it allocates a stack frame, which ultimately leads to a stack overflow.
, , . , , . :
void f (void) {
char str[10];
strcpy (str, "This is far too long to fit");
}
, , , 27- (28 ) 10 .
. (, malloc), , , :
void f (void) {
char *blk = malloc (10);
if (blk != 0) {
memset (blk, ' ', 100);
free (blk);
}
}
, , . .