So, I am new to C++. I've researched Segmentation Fault (core dumped), memory allocation, and new/delete although I am having trouble understanding the concepts. I do believe my problem lies with memory allocation though and that's why I thought "delete" would have solved my problem. May someone please push me in the right direction?
Input
#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
string name;
double weight;
int calories;
} ;
int main()
{
CandyBar* backpack = new CandyBar[3];
backpack[0] = {"Choco Chalk", 5.1, 725};
backpack[1] = {"Twisty String", 1.8, 300};
backpack[2] = {"Gummy Nums", 4.4, 475};
cout << "Name\t\tWeight\tCalories" << endl << endl;
for (int x = 0; x < 4; x++)
{
cout << backpack[x].name << "\t" << backpack[x].weight << "\t" << backpack[x].calories << endl;
}
delete backpack;
return 0;
}
Output
Name Weight Calories
Choco Chalk 5.1 725
Twisty String 1.8 300
Gummy Nums 4.4 475
(Segmentation fault) core dumped