Exception thrown at 0x793F3729 (vcruntime140d.dll) in filePath.exe: 0xC0000005: Access violation writing location 0xCDCDCDCD

0

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;
        }
};
c++
visual-studio
exception
linked-list
stack
asked on Stack Overflow Apr 25, 2020 by TuTu

3 Answers

1

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;
answered on Stack Overflow Apr 25, 2020 by john
1

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.

answered on Stack Overflow Apr 25, 2020 by nanofarad • edited Apr 25, 2020 by nanofarad
1

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);
answered on Stack Overflow Apr 25, 2020 by emaditaj

User contributions licensed under CC BY-SA 3.0