System.AccessViolationException试图读取或写入受保护的内存

System.AccessViolationException Attempted to read or write protected memory

本文关键字:受保护 内存 AccessViolationException 读取 System      更新时间:2023-10-16

嗨,我正在开发一个使用链表数组的程序,但我在运行它时遇到了问题。我一直收到这个错误,但我找不到修复它的方法。我只会包含部分代码,这样一切都不会太乱。错误消息说NodeADT.h中的第112行、MultiListADT.h的第141行和main.cpp中的第21行是抛出错误的行。我将突出显示这些线条,使其更容易。

主要.cpp

#include <iostream>
#include "MultiListADT.h"
#include <fstream>
#include <string>
using namespace std;
void main(void)
{
MultiListADT<string,100> myList;
string item;
ifstream data;
string input;
int x=0;
data.open("input.txt");
while (!data.eof())
{
    getline(data,input);
    myList.AddToFront(input);        //This is line 21
}
cout << myList << endl;
system("pause");
}

MultiListADT.h

#include <iostream>
#include <fstream>
#include "NodeADT.h"
#include <string>
using namespace std;
template <class TYPE,int threads>
class MultiListADT
{
public:
/** Constructor **/
MultiListADT();
/** Destructor **/
~MultiListADT();
/** Declare accessors (observers) **/
void ResetListForward(int=0);
void ResetListBackward(int=0);
bool IsEmpty(int=0);
int LengthIs(int=0);
bool Search(string, bool=true,int=0);
void GetNextItem(TYPE &,int i=0);
void GetPreviousItem(TYPE &,int=0);
int GetInfo(int=0);
friend ostream& operator << (ostream&, MultiListADT<TYPE, 100>&);
/** Declare mutators (transformers) **/
void MakeEmpty();
void AddToFront(TYPE);
void AddToRear(TYPE);
void InsertInOrder(TYPE);
void Delete(TYPE);
void Sort();
private:
NodeADT<TYPE,threads>* head[threads];
NodeADT<TYPE,threads>* tail[threads];
int length;
string indices[threads];
NodeADT<TYPE,threads>* currentNode[threads];
};
template <class TYPE,int threads>
MultiListADT<TYPE,threads>::MultiListADT()
{
head[threads] = new NodeADT<string,threads>(); 
tail[threads] = new NodeADT<string,threads>();
head[threads]->setNext(tail[threads]);
tail[threads]->setPrevious(head[threads]);
length = 0;
}
template <class TYPE,int threads>
void MultiListADT<TYPE,threads>::AddToFront(TYPE item)
{
head[0]->AddToFront(item);        //This is line 141
length++;
}

NoteADT.h

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int null = 0;
template<class TYPE, int threads>
class MultiListADT;
template <class TYPE, int threads>
class NodeADT
{
public:
NodeADT();
NodeADT(TYPE);
~NodeADT();
TYPE getInfo();
NodeADT<TYPE, threads>* getPrevious(int=0);
NodeADT<TYPE, threads>* getNext(int=0);
void setNext(NodeADT<TYPE, threads>*,int=0);
void setPrevious(NodeADT<TYPE, threads>*,int=0);
bool Search(TYPE, bool=true,int=0);
void AddToFront(TYPE item);
void AddToRear(TYPE item);
void InsertInOrder(TYPE);
bool Delete(TYPE);
friend ostream& operator << (ostream&, MultiListADT<TYPE, threads>&);
private:
TYPE info;
NodeADT<TYPE, threads>* prev[threads];
NodeADT<TYPE, threads>* next[threads];
};
template <class TYPE,int threads>
NodeADT<TYPE,threads>::NodeADT()
{
prev[threads] = null;
next[threads] = null;
}
template <class TYPE,int threads>
NodeADT<TYPE,threads>::NodeADT(TYPE item)
{
info = item;
prev = null;
next = null;
}
template <class TYPE,int threads>
void NodeADT<TYPE,threads>::AddToFront(TYPE item)
{
NodeADT<TYPE,threads> *temp = new NodeADT<TYPE,threads>;
temp->info = item;  
temp->prev[0] = this;
temp->next[0] = next[0];
next[0]->prev[0] = temp;        //This is line 112
next[0] = temp;
}

您认为错误意味着什么?

在第112行,next、prev和temp的值从哪里来?当它崩溃时,它们被设置为什么?知道这些价值观,你认为它为什么会崩溃?

此外,在一个NodeADT构造函数中,您将null分配给数组的最后一个元素。或者看起来是这样。

问题:当元素计数从0开始时,为100个元素的数组中编号为100的元素赋值时会发生什么?

我认为Zan-Lynx暗示的答案是,在MultiListADT的构造函数中使用threads作为数组的索引。在AddToFront中,您使用0作为索引,但数组中的元素从未初始化过。