unhandled exception
Nov 1, 2013 at 8:16pm UTC
Hey everyone I'm working on a Doubly linked list for my comp sci class, its coming along well but I keep getting this error: "Unhandled exception at 0x00B547E9 in Project2.exe: 0xC0000005: Access violation reading location 0xCCCCCCD0."
program name for now is program 2.
here is the code I have so far. I havent split it into header files yet so it a little lengthy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class node
{
friend class list;
public :
int data;
node* next;
node* prev;
};
class list
{
friend class node;
public :
node* head;
node* tail;
void addright(int );
void addleft(int );
int asker();
int printl();
//list();
};
/* list::list()
{
head->next = NULL;
tail->prev = NULL;
}*/
string x;
int a;
int list::asker()
{
cout << "list> " ;
cin >> x;
if (x == "addleft" ) {cin >> a; addleft(a);}
else
if (x == "addright" ) {cin >> a; addright(a);}
else
if (x == "quit" ) return 0;
return 1;
}
void list::addleft(int a)
{
node* temp = new node;
temp->data = a;
if (head->next == NULL)
{
head->next = temp;
temp->prev = head;
tail->prev = temp;
temp->next = tail;
return ;
}
node* c;
c = head;
head->next = temp;
temp->next = c->next;
temp->prev = head;
c = c->next;
c->prev = temp;
}
void list::addright (int a)
{
}
int list::printl()
{
node* c;
c = head->next;
if (c == NULL){ cout << "Error!" ; return 0;}
else while (c->next != NULL)
cout << c->data;
c = c->next;
return 0;
}
int main()
{
list my_list;
int num = 1;
do
{
num = my_list.asker( );
} while (num);
return 0;
}
thanks for any help you can offer
Nov 1, 2013 at 8:22pm UTC
0xCCCCCCD0 looks like an offset of 0xCCCCCCCC, which is what MS compilers set uninitialized data to in debug mode. This means you are trying to dereference an uninitialized pointer.
Nov 1, 2013 at 8:29pm UTC
This if (head->next == NULL)
is a problem if head is null.
Last edited on Nov 1, 2013 at 8:31pm UTC
Topic archived. No new replies allowed.