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

删除动态数组触发heap corruption错误,求原因及解决方法

堆损坏(heap corruption detected)原因及修复方案

错误原因

你的代码核心问题出在内存分配时机错误:

  • 初始化nums时,SIZE的值是0,此时new double[SIZE]分配的是一个长度为0的空堆数组,没有任何可用的存储空间。
  • 之后你通过cin >> SIZE修改了SIZE的数值,但nums指向的内存空间大小并不会自动更新。当你循环往nums[i]写入数据时,本质是在越界访问堆内存——写入的内容会覆盖堆内存的管理元数据(比如内存块的大小标记、链表指针等)。
  • 当执行delete[] nums时,堆分配器检查到管理元数据被破坏,就会触发heap corruption detected错误并终止程序。

修复方案

方案1:调整内存分配时机(最直接)

先让用户输入SIZE,再用正确的SIZE值分配内存,确保数组大小和实际需要存储的元素数量匹配:

#include <iostream>
using namespace std;

int main()
{
    cout << "Hi, Let me find the Average! \n \n";
    
    int SIZE = 0;
    double *nums = nullptr; // 先初始化为空指针,避免野指针
    int i = 0;
    double total = 0.0;
    double num = 0.0;
    double average = 0.0;
    
    // 先获取用户输入的SIZE,再分配对应大小的内存
    cout << "Please enter how many numbers to be entered: ";
    cin >> SIZE;
    nums = new double[SIZE];
    
    do{
        cout << "Please enter the " <<i+1<< "st/th number : ";
        cin >> num;
        nums[i] = num;
        num = 0;
        i++;
        if (i == SIZE) break;
    } while (i < SIZE);
    
    cout << "Here are the numbers you entered: \n";
    for (int n = 0; n < SIZE; n++) {
        cout << nums[n] << "\n";
    }
    
    for (int n = 0; n < SIZE; n++) {
        total += nums[n];
    }
    
    cout << "You chose to enter " << SIZE << " numbers. \n";
    cout << "The total of all the numbers you entered is : " << total << "\n";
    average = total / double(SIZE);
    cout << "The average of all the numbers that you added is: " << average << "\n";
    
    delete[] nums; // 现在可以安全执行释放
    nums = nullptr; // 将指针置空,避免后续误操作
}

方案2:使用vector自动管理内存(更安全)

C++标准库的vector会自动处理内存分配和释放,完全避免手动管理指针的风险:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    cout << "Hi, Let me find the Average! \n \n";
    
    int SIZE = 0;
    vector<double> nums; // 用vector替代手动分配的数组
    int i = 0;
    double total = 0.0;
    double num = 0.0;
    double average = 0.0;
    
    cout << "Please enter how many numbers to be entered: ";
    cin >> SIZE;
    nums.reserve(SIZE); // 预分配空间,提升性能(可选)
    
    do{
        cout << "Please enter the " <<i+1<< "st/th number : ";
        cin >> num;
        nums.push_back(num); // 直接添加元素,vector自动扩容
        num = 0;
        i++;
        if (i == SIZE) break;
    } while (i < SIZE);
    
    cout << "Here are the numbers you entered: \n";
    for (double n : nums) { // 范围for循环遍历更简洁
        cout << n << "\n";
    }
    
    for (double n : nums) {
        total += n;
    }
    
    cout << "You chose to enter " << SIZE << " numbers. \n";
    cout << "The total of all the numbers you entered is : " << total << "\n";
    average = total / double(SIZE);
    cout << "The average of all the numbers that you added is: " << average << "\n";
    
    // 无需手动执行delete,vector会在生命周期结束时自动释放内存
}

补充说明

堆内存分配器会在每个分配的内存块前后存储管理元数据(比如内存块的大小、下一个空闲块的地址等)。当你越界写入数据时,这些元数据会被破坏,导致分配器无法正确识别内存块的边界,最终在释放内存时触发堆损坏检测。

内容的提问来源于stack exchange,提问作者Kate Rainey

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 15:47:50