c ++ munmap_chunk():指针错误无效

时间:2017-08-12 17:32:07

标签: c++

在我的程序中,我有一个动态分配的结构数组。我立即初始化所有值,然后打印它们。之后我删除整个数组。该程序很简单,它的工作原理。但是在它结束打印最后一个结构的值之后,它也会输出错误(' 9'是程序名称):

  

**错误' ./ 9':munmap_chunk():ivalid pinter:0x00e34010 ***

     

中止

我读到了这个错误,当你尝试双重释放内存或访问错误的扫描仪时会发生这种错误,但是我的代码中没有看到错误的antyhing。这是:

#include <iostream>

using namespace std;

struct CandyBar
{
    string brand;
    double weight;
    int calories;
};

int main()
{
    CandyBar *candyArr = new CandyBar [3]
    {
        {"Mandy", 3.2, 220},
        {"Bandy", 5.2, 102},
        {"Candy", 22.3, 209}
    };

    cout << "1: " << (*candyArr).brand << ", " << (*candyArr).weight << ", " << (*candyArr).calories << endl;
    cout << "2: " << (*(candyArr+1)).brand << ", " << (*(candyArr+1)).weight << ", " << (*(candyArr+1)).calories << endl;
    cout << "3: " << (*(candyArr+2)).brand << ", " << (*(candyArr+2)).weight << ", " << (*(candyArr+2)).calories << endl;

    delete candyArr;
    return 0;
}

0 个答案:

没有答案