Reputation: 17
I'm attempting to implement a linked list as a stack so I created on a custom class for it in C++. Below is what I used to push the temp1 variable into the stack. My current issue is when I push into the stack, it throws an exception.
cityNode temp1(dest, cost + dataVector[index].cost, time + dataVector[index].time);
stack->pushCity(temp1);
This is the node.
class cityNode {
public:
string name; // City name
double totalCost; // Total cost of the flight
int totalTime; // Total time of the flight
cityNode* destCity; // Pointer to the next destionation city
cityNode(string cityName, double cityCost, int cityTime) {
name = cityName;
totalCost = cityCost;
totalTime = cityTime;
destCity = nullptr;
}
};
Below is my code. The exception is currently thrown when it reaches the newCity->totalCost = cityNode.totalCost;
part of the code.
class CityStack {
public:
class cityNode* top = NULL;
void pushCity(cityNode cityNode) {
class cityNode* newCity = (class cityNode*) malloc(sizeof(class cityNode));
newCity->name = cityNode.name;
newCity->totalCost = cityNode.totalCost; // Exception Thrown
newCity->totalTime = cityNode.totalTime;
newCity->destCity = top;
top = newCity;
}
void popCity() {
if (top == NULL) {
cout << "Stack Underflow" << endl;
}
else {
top = top->destCity;
}
}
bool emptyCity() {
if (top == NULL)
return true;
else
return false;
}
void reverseCity() {
cityNode *prev, *cur, *succ;
cur = prev = top;
//cur = cur->destCity;
while (cur != NULL) {
cityNode *temp = cur->destCity;
delete cur;
cur = temp;
}
//prev->destCity = NULL;
while (prev != NULL) {
cityNode *temp = prev->destCity;
delete prev;
prev = temp;
}
while (cur != NULL) {
succ = cur->destCity;
cur->destCity = prev;
prev = cur;
cur = succ;
}
top = prev;
}
void displayCities() {
cityNode *stack = top;
while (stack != NULL) {
cout << stack->name << " ";
stack = stack->destCity;
}
cout << endl;
}
cityNode* getCityNode() {
return top;
}
};
Upvotes: 0
Views: 2878
Reputation: 41271
0xcdcdcdcd
is used by Visual Studio to mark uninitialized heap memory; you read such uninialized memory and thus get this invalid pointer. You should be using new
, not malloc
in C++:
void pushCity(cityNode cn) {
cityNode* newCity = new cityNode(cn.name, cn.totalCost, cn.totalTime);
newCity->destCity = top;
top = newCity;
}
new
will allocate the correct amount of memory, and will invoke your constructor appropriately. Meanwhile, malloc
will just allocate a bit of memory in the correct size, but do nothing to initialize the object. This leads to undefined behavior.
Upvotes: 1
Reputation: 177
as others have mentioned use new instead but if you insist on allocating memory with malloc for any reason (for example avoiding exception when new can't allocate memory for anyreason like not enouth memory)
you need to call constructor explicitly by "replacement new"
class cityNode* newCity = (class cityNode*) malloc(sizeof(class cityNode));
if(newCity ==0)
{throw("can't allocate memory");}
new(newCity) cityNode();
...
//you need to call destructor expilictly too
newCity.~cityNode();
//you still must use free() not delete
free(newCity);
Upvotes: 1
Reputation: 87944
Don't use malloc
in a C++ program (unless you really know what you are doing) because it doesn't call the constructors for the objects you are creating.
Replace this
class cityNode* newCity = (class cityNode*) malloc(sizeof(class cityNode));
with this
cityNode* newCity = new cityNode;
Upvotes: 1