You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用CodeBlocks编写C++程序时出现a.exe已停止运行错误求助

Why Your C++ Program is Crashing with "a.exe has stopped working"

Hey there! Let's break down exactly what's causing your program to crash and how to fix it quickly.

The Root Cause

Your code declares a 1000x1000 integer array int mat[1000][1000]; on the stack—the small, limited memory space reserved for local variables. Stack sizes are typically pretty tight (most systems default to 1MB-8MB). Let's do the math: each int takes up ~4 bytes, so 100010004 = 4,000,000 bytes (~4MB). If your compiler's default stack size is smaller than that (even GCC in CodeBlocks can hit system-enforced limits here), this array is pushing the stack over its capacity—resulting in a crash.

Fixes to Try

Here are several straightforward solutions to resolve this issue:

Swap your stack-allocated array for a std::vector, which uses heap memory (a much larger pool) instead of the stack. This is the most idiomatic C++ approach and handles memory management automatically.

#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<vector<int>> mat(1000, vector<int>(1000));
    cout << "done";
    return 0;
}

2. Mark the Array as static

Adding the static keyword moves the array from the stack to static storage (which has way more available memory). This is a quick fix, but note that static variables persist for the entire program's lifetime—use this cautiously if you need reentrant code or multiple instances.

#include <bits/stdc++.h>
using namespace std;

int main() {
    static int mat[1000][1000];
    cout << "done";
    return 0;
}

3. Allocate on the Heap Manually

You can use new to allocate the array directly on the heap. Just remember to clean up the memory to avoid leaks (though the OS will reclaim it when the program exits in this simple case):

#include <bits/stdc++.h>
using namespace std;

int main() {
    int** mat = new int*[1000];
    for (int i = 0; i < 1000; ++i) {
        mat[i] = new int[1000];
    }

    cout << "done";

    // Clean up allocated memory
    for (int i = 0; i < 1000; ++i) {
        delete[] mat[i];
    }
    delete[] mat;

    return 0;
}

4. Increase the Stack Size (Advanced)

If you really need a stack-allocated array, you can adjust CodeBlocks' linker settings to expand the stack:

  • Go to Project > Build Options > Linker Settings
  • Under Other linker options, add -Wl,--stack,8388608 (this sets the stack size to 8MB; adjust the number if you need more space)

Which Fix Should You Pick?

  • std::vector is the best all-around choice—it's safe, easy to maintain, and avoids portability issues.
  • static is a quick band-aid but not ideal for reusable or complex code.
  • Manual heap allocation gives you control but requires careful memory management.
  • Stack size adjustment is system-dependent and makes your program less portable.

Try any of these solutions, and your program should run without crashing!

内容的提问来源于stack exchange,提问作者Piyush Soni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:37:09