0

In my program I have one dynamically allocated array of structures. I initialize all the values immediately and then print them. After it I delete whole array. The program is simple and it works. But after it end printing values of last structure, it also prints an error ('9' is the program name):

** Error in './9': munmap_chunk(): ivalid pinter: 0x00e34010 ***

Aborted

I read about this error and it occurs when you try to double free the memory or to access sth in a wrong wat, but I don't see antyhing wrong in my code. Here it is:

#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;
}
Community
  • 1
  • 1
RocketBall
  • 45
  • 6

0 Answers0