'c++: Linked list error " Access violation reading location"
I'm trying to create a function for my linked list that adds a node to the end of my program. I'm getting an error when using the the link forward in my node that states
Access violation reading location 0xccccccd8
It's pointing the line at the start of the while loop inside my add function. How can I fix this?
My add, print and showList function:
void List::Add(char data, char* dataString)
{
Node* n5;
n5 = new Node;
n5->data = data;
n5->dataString = dataString;
while (nodes->linkf != NULL)
{
nodes = nodes->linkf;
}
nodes->linkb = n5;
//n5->linkb = List::nodes;
//n5->linkf = NULL;
}
void List::showList(int dir)
{
if (dir==1){
while (nodes !=NULL)
{
Print();
nodes = nodes->linkf;
}
cout<<"\n";
}
if (dir==0){
while(nodes != NULL)
{
Print();
nodes = nodes->linkb;
}
cout<<"\n";
}
}
void List::Print()
{
cout<<" \n";
cout<<nodes->data;
cout<<nodes->dataString;
cout<<" \n";
}
Node and List classes:
class Node
{
public:
Node(){
char data[5];
dataString=new(char[10]);
}
~Node(){};
Node *linkb;
char data;
char* dataString;
Node *linkf;
};
class List{
public:
Node* nodes;
List(){}
void Add(char data, char*dataString);
void showList(int);
void Print();
string entry;
};
Main:
Node *n1, *n2, *n3, *n4 ;
List *l1;
int direction;
char choice;
char data;
char* dataString = "";
char dataBuffer[30];
List myList;
n1 = new(Node);
n2 = new(Node);
n3 = new(Node);
n4 = new(Node);
string entry= "";
l1 =new(List);
cout << "Please input the data: " << endl;
cin >> data;
cout << "Please input the data string: " << endl;
cin >> dataBuffer;
dataString = &dataBuffer[0];
n1->linkb = NULL;
n1->data='C';
n1->dataString="ats ";
n1->linkf = n2;
n2->linkb = n1;
n2->data='L';
n2->dataString="ike ";
n2->linkf = n3;
n3->linkb = n4;
n3->data='F';
n3->dataString="ish ";
n3->linkf = NULL;
n4->linkb = n2;
n4->data='D';
n4->dataString="ont ";
n4->linkf = NULL;
l1->nodes = n1;
myList.Add(data, dataString);
cout<<"\nShow linked list Forward (F) or Backward (B): ";
cin>>choice;
if (choice == 'F')
{
direction=1;
l1->nodes=n1;
l1->showList(direction);
}
else if (choice=='B')
{
direction=0;
l1->nodes=n3;
l1->showList(direction);
}
else
{
cout<<"INVALID ENTRY !";
exit(1);
}
delete (n1);
delete (n2);
delete (n3);
delete (n4);
Solution 1:[1]
The problem is here:
When you call myList.Add(data, dataString);, the content of myList.nodes is undetermined because nobody has ever initialized it.
void List::Add(char data, char* dataString)
{
Node* n5;
n5 = new Node;
n5->data = data;
n5->dataString = dataString;
while (nodes->linkf != NULL) // nodes is undetermined here and therefore
// dereferencing it crashes the program
{
nodes = nodes->linkf;
}
nodes->linkb = n5;
//n5->linkb = List::nodes;
//n5->linkf = NULL;
}
If found this out in 30 seconbds using (guess what) the debugger.
But there are most likely more problems elsewhere in your code, the whole code looks fishy.
Solution 2:[2]
Looking at your code you never set your pointers to null, which means they'll just take whatever garbage value is in memory at time of construction. In your node class constructor, you should initialise all pointers to null:
Node()
: linkb(nullptr), linkf(nullptr)
{
char data[5];
dataString=new(char[10]);
}
You should do the same for the list class
Solution 3:[3]
So I just solved it although there's still a lot of errors and badly written code it technically works. When I was declaring the nodes equal to n1 in main I was using a different List object to the add function.
My code before:
l1->nodes = n1;
myList.Add(data, dataString);
My code now:
l1->nodes = n1;
l1->Add(data, dataString);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jabberwocky |
| Solution 2 | |
| Solution 3 | Eoin Coogan |
