【问题标题】:Exception thrown at 0x793F3729 (vcruntime140d.dll) in filePath.exe: 0xC0000005: Access violation writing location 0xCDCDCDCD在 filePath.exe 中的 0x793F3729 (vcruntime140d.dll) 处引发异常:0xC0000005:访问冲突写入位置 0xCDCDCDCD
【发布时间】:2020-08-09 10:41:08
【问题描述】:

我正在尝试将链表实现为堆栈,因此我在 C++ 中为它创建了一个自定义类。 下面是我用来将 temp1 变量推入堆栈的内容。我当前的问题是当我压入堆栈时,它会引发异常。

cityNode temp1(dest, cost + dataVector[index].cost, time + dataVector[index].time);
stack->pushCity(temp1);

这是节点。

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;
        }
};

下面是我的代码。当前在到达代码的newCity->totalCost = cityNode.totalCost; 部分时会抛出异常。

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


    【解决方案1】:

    0xcdcdcdcd被Visual Studio用来标记未初始化的堆内存;您读取了此类未初始化的内存,从而获得了此无效指针。你应该在 C++ 中使用new,而不是malloc

    void pushCity(cityNode cn) {
      cityNode* newCity = new cityNode(cn.name, cn.totalCost, cn.totalTime);
      newCity->destCity = top;
      top = newCity;
    }
    

    new 将分配正确的内存量,并适当地调用您的构造函数。同时,malloc 只会分配一些正确大小的内存,但不做任何初始化对象的操作。这导致undefined behavior

    【讨论】:

    • 当我这样做时,它给了我这个结果:identifier "newCity" is undefined
    • @ThuVu 不要让参数名和类名一样。我将编辑代码并进行完整修复
    • @ThuVu 试试写的代​​码;它现在应该可以工作了,因为参数的名称与类的名称没有冲突
    【解决方案2】:

    正如其他人所提到的,使用 new 代替 但是如果您出于任何原因坚持使用 malloc 分配内存(例如,当 new 无法为任何原因分配内存时避免异常,例如没有足够的内存)

    你需要通过“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);
    

    【讨论】:

      【解决方案3】:

      不要在 C++ 程序中使用malloc(除非您真的知道自己在做什么),因为它不会为您正在创建的对象调用构造函数。

      替换这个

      class cityNode* newCity = (class cityNode*) malloc(sizeof(class cityNode));
      

      有了这个

      cityNode* newCity = new cityNode;
      

      【讨论】:

        猜你喜欢
        • 2021-07-03
        • 2020-02-13
        • 2018-06-26
        • 1970-01-01
        • 2015-08-15
        • 2016-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多