posts - 137,comments - 0,views - 40818

当调用一个函数时,就会在栈空间为这个函数分配一块内存区域,这块内存区域叫做“栈帧”,专门给这个函数使用。 

在调用函数时要避免栈空间溢出,否则将会引起程序异常。

 

 示例一:

复制代码
#include <iostream>
#include <windows.h>
using namespace std;
void test() {
    //运行时将因为栈帧空间溢出而崩溃
    char buff[2000000];
    std::cout << (int)buff[sizeof(buff) - 1] << std::endl;
}
int main() {
    test();
    system("pause");
    return 0;
}
复制代码

 示例二:

复制代码
#include <iostream>
#include <windows.h>
using namespace std;
void test2(int n) {
    char buff[1024 * 100]; //100K
    printf("n=%d\n", n);
    printf("%X\n", buff);
    if (n == 1) {
        return;
    }
    test2(n - 1);
}
int main(void) {
    test2(3);
    system("pause");
    return 0;
}
复制代码

 

posted on   wshidaboss  阅读(113)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
· Manus的开源复刻OpenManus初探
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示