/* * Allocate too much memory from stack will cause stack overflow. */
#include stdio.h
int main(int argc, char *argv[]) { int foo[1000000]; return 0; }
C:\ cl stack_local.c Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved.
stack_local.c Microsoft (R) Incremental Linker Version 8.00.50727.42 Copyright (C) Microsoft Corporation. All rights reserved.
/out:stack_local.exe stack_local.obj
C:\ stack_local
此时出现一个异常对话框:stack-local.jpg 。
2、函数递归调用导致的栈溢出
C:\ more stack_recursive.c
/* * Infinite recursive calls will lead to stack overflow soon. */
#include stdio.h
static void foo(void); static void bar(void);
int main(int argc, char *argv[]) { foo(); return 0; }
static void foo(void) { bar(); }
static void bar(void) { foo(); }
C:\ cl stack_recursive.c Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved.
stack_recursive.c Microsoft (R) Incremental Linker Version 8.00.50727.42 Copyright (C) Microsoft Corporation. All rights reserved.