I have a customer node and an item node structure and I'm trying to test them. I make a customer node and add some items to its basket. However, when I'm deleting the nodes, the program mostly crashes. I'm trying to first delete the basket and then delete the customer node. The code sometimes runs fine but sometimes crashes. Why do you think it crashes? Your help is appreciated.
My code is as follows:
#include <iostream>
#include <string>
struct itemNode
{
int id;
char *itemName;
int amount;
itemNode *next;
itemNode(const int id, const char *name, const int amount, itemNode *const next = nullptr)
: id(id), amount(amount), next(next)
{
this->itemName = new char[strlen(name)];
strcpy(this->itemName, name);
std::cout << "Item " << this->itemName << " constructed." << std::endl;
};
~itemNode()
{
std::cout << "Deleting " << itemName << std::endl;
delete[] this->itemName;
if (this->next != nullptr)
{
delete this->next;
}
}
};
struct customerNode
{
int id;
char *fullName;
itemNode *basket;
customerNode *next;
customerNode *previous;
customerNode(const int id, const char *name, customerNode *const previous, customerNode *const next)
: id(id), previous(previous), next(next)
{
this->fullName = new char[strlen(name)];
strcpy(this->fullName, name);
std::cout << "Customer " << this->fullName << " constructed." << std::endl;
this->basket = nullptr;
this->next = next;
this->previous = previous;
};
~customerNode()
{
std::cout << "Deleting " << fullName << std::endl;
delete[] this->fullName;
if (this->basket != nullptr)
{
delete this->basket;
}
}
};
int main()
{
customerNode *customer1 = new customerNode(14235, "Georges Politzer", nullptr, nullptr);
itemNode *item3 = new itemNode(5355, "Pen", 2, nullptr);
itemNode *item2 = new itemNode(1351, "Folder Case", 1, item3);
itemNode *item1 = new itemNode(4412, "Paper Clips", 20, item2);
customer1->basket = item1;
delete customer1->basket;
customer1->basket = nullptr;
delete customer1;
return 0;
}