读取访问冲突 0xCDCDCDCD [英] Read access violation 0xCDCDCDCD

查看:58
本文介绍了读取访问冲突 0xCDCDCDCD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到读取访问冲突.这是我的代码.

I keep getting a read access violation. Here is the code that I have.

class List
{
public:
    List();
    List(const List &copy);
    ~List();

    /*List & operator=(const List &rhs);


    Record * headPtr() const;
    void setheadptr(Record * const newhead);

    bool insertatfront(Record newdata);*/


    void importcourselist();
    void Loadmasterlist();
    void storemasterlist();

    Record *makenode(fstream &file);
    Record *makenode(ifstream &file);

private:
    Record *mHead;
    Record *mEnd;

};
List::List()
{
    mHead = nullptr;
    mEnd = nullptr;

}

void List::storemasterlist()
{
    ofstream outfile;
    outfile.open("masterlist.txt");

    mEnd = mHead;
    while (mEnd->getnext()!= nullptr)
    {
        outfile << mEnd << endl;
        mEnd = mEnd->getnext();

    }
    outfile << mEnd;
}

我不太确定发生了什么,但是,每次我尝试调试它时,它都会将我发送到我的指针的 getter 函数,如下所示:

I'm not exactly sure what is going on but, every time I try to debug it it'll send me to the getter function for my pointer which looks like this:

 Record *Record::getnext()
    {
        return mnext;
    }

我相信我的错误存在于我的导入函数中.这是我创建额外空节点的地方.

I believe that my error lies somewhere in my import function. This is where I'm creating the extra empty node.

void List::importcourselist()
{
    ifstream infile;
    string ptoken;
    infile.open("studentlist.csv");

    getline(infile, ptoken);

    mHead = makenode(infile);

    mEnd = mHead;



    while (!infile.eof())
    {
        mEnd->setnext(makenode(infile));
        mEnd = mEnd->getnext();
    }

    infile.close();
}

推荐答案

常量 0xCD 被 Microsoft C++ 调试库用来填充分配的堆块.因此,当您从尚未由您的代码初始化的动态堆对象中读取数据时,您可以看到模式 0xCDCDCDCD.

The constant 0xCD is used by Microsoft C++ debug libraries to fill allocated heap blocks. So you can see the pattern 0xCDCDCDCD when you read data from a dynamic heap object which hasn't been initialized by your code.

您没有显示 Record 类定义,但我猜您的 Record::Record 构造函数未能将 next 指针设置为nullptr.当迭代到达列表的末尾时,您使用 getnext() 获取神奇的 0xCDCDCDCD 值.这是一个无效的指针,因此您无法通过下一个 getnext() 调用访问 0xCDCDCDCD 处的内存.

You didn't show the Record class definition, but I guess your Record::Record constructor fails to set the next pointer to nullptr. When iteration comes to the end of the list you fetch that magic 0xCDCDCDCD value with getnext(). That is an invalid pointer, hence you fail to access the memory at 0xCDCDCDCD with the next getnext() call.

来源:

  • Magic number (programming) at Wikpedia
  • Win32 Debug CRT Heap Internals by Andrew Birkett at his www.nobugs.org
  • Magic Numbers in Visual C++ (Aug 2009) at Adam Sawicki's asawicki.info

这篇关于读取访问冲突 0xCDCDCDCD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆