1

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

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();
}
bigpimpin206
  • 17
  • 1
  • 5

2 Answers2

4

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.

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.

Sources:

CiaPan
  • 8,142
  • 2
  • 18
  • 32
0

This is sort of a guess:

  • you call while(mEnd->getnext() ...) for the loop
  • then in the body of the loop you call mEnd->getnext() again, which does not deliver the same result; i.E. it might be past the end already.
user23573
  • 2,372
  • 1
  • 14
  • 32