gpt4 book ai didi

c++ - 访问冲突读取位置 0xCDCDCDD1

转载 作者:太空宇宙 更新时间:2023-11-04 16:13:11 25 4
gpt4 key购买 nike

我正在使用 Visual Studio 2013。我收到了 Access violation reading location 0xCDCDCDD1 错误。引用资料来自 Wikipedia: Magic Numbers , here , 和 this question告诉我我正在使用未初始化的指针。我认为问题出在我正在使用的 malloc 函数上,尽管我确实引用了 CPlusPlus在使用它之前先文档。

我的代码:

#include "SingleNode.h"
#include "SingleList.h"
#include <iostream>
SingleList::SingleList() {}
SingleList::~SingleList() {}
...
void SingleList::addHead(int value){
ISingleNode *temp1 = (ISingleNode *)malloc(sizeof(SingleList));
head = NULL;
temp1->setValue(value);
temp1->setNext(head->getNext());
if (!head->getNext())
{
head = temp1;
}
else
{
temp1->setNext(head->getNext());
head = temp1;
}
}

...

错误在行 temp1->setValue(value);

setValue(int value) 函数包含在此处:

#include "SingleNode.h"
SingleNode::SingleNode() {}
SingleNode::~SingleNode() {}
void SingleNode::setValue(int value){
this->value = value;
}
int SingleNode::getValue(){
return value;
}
ISingleNode * SingleNode::getNext(){
return next;
}
void SingleNode::setNext(ISingleNode * next){
this->next = next;
}

单节点.h:

#pragma once
#include "Interfaces01.h"
class SingleNode :public ISingleNode{
private:
int value;
ISingleNode * next = NULL;
public:
SingleNode();
~SingleNode();
void setValue(int value);
int getValue();
ISingleNode * getNext();
void setNext(ISingleNode * next);
};

单个列表.h:

#pragma once
#include "Interfaces01.h"
class SingleList :public ISingleList{
private:
int value;
ISingleNode * head=NULL;
ISingleNode * temp=NULL;
ISingleNode * next=NULL;
ISingleNode * temp1;
public:
SingleList();
~SingleList();
ISingleNode * getHead();
void setHead(ISingleNode * head);
void addHead(int value);
void orderSort2();
void orderSort3();
void sequenceOrderSort();
void reverse();
ISingleNode * swap(ISingleNode * head, int k);
ISingleNode * get_node(ISingleNode * head, int k);
};

断行时,调试器向我显示以下值:

Visual Studio 2013 debug values at breakpoint

最佳答案

  1. 改变:

    ISingleNode *temp1 = (ISingleNode *)malloc(sizeof(SingleList));

    进入:

    ISingleNode *temp1 = new SingleList;
  2. 您正在访问一个 NULL 指针:

    head = NULL; // set to NULL
    temp1->setValue(value);
    temp1->setNext(head->getNext()); // oops!

    你的意思可能是:

    void SingleList::addHead(int value)
    {
    ISingleNode *temp1 = new SingleList;
    temp1->setValue(value);
    temp1->setNext(head);
    head = temp1;
    }

关于c++ - 访问冲突读取位置 0xCDCDCDD1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25850443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com