Access violation writing location

I am getting an access violation writing location on line 267. The first document it read has the numbers 1 - 100. The second document is blank. It is checking to see if 1 is odd and then placing it in the odd doubly linked list. As soon as it gets to line 267 its giving me a Access violation writing location. I have verified val is indeed an int of 1. It is also giving me the same error when i try to add an even number to the even doubly linked list. Any help would be much appreciated. A side not is this is for a class and we cannot use any class's or OOP.

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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <stack>
#include <fstream>
#include <cmath>
using namespace std;

// -- Even doubly link list ------------------------------------
struct evenLink {
	evenLink *prev;
	int integer;
	evenLink *next;
};

struct evenInfo {
	evenLink *top;
	int length;
};
// -------------------------------------------------------------

// -- Odd doubly link list -------------------------------------
struct oddLink {
	oddLink *prev;
	int integer;
	oddLink *next;
};

struct oddInfo {
	oddLink *top;
	int length;
};
// -------------------------------------------------------------

// -- GLOBAL CONST ---------------------------------------------
const string FILE_ONE_ERROR = "Please enter a valid first file: ";
const string FILE_TWO_ERROR = "Please enter a valid second file: ";
const string EVEN_COUNT = " integers inserted into Even list:";
const string ODD_COUNT = " integers inserted into Odd list:";
const int ONE = 1;
const int TWO = 2;
// -------------------------------------------------------------

// -- EVEN ADT -------------------------------------------------
bool EmptyListEven(evenInfo *newStack);
int ListLengthEven(evenInfo *newNode);
void DisplayListEven(evenInfo *newStack);
void OrderedListInsertionEven(int val, evenInfo *newList);
// -------------------------------------------------------------

// -- ODD ADT --------------------------------------------------
bool EmptyListOdd(oddInfo *newStack);
int ListLengthOdd(oddInfo *newNode);
void DisplayListOdd(oddInfo *newStack);
void OrderedListInsertionOdd(int oddVal, oddInfo *newList);
// -------------------------------------------------------------

// -- Prototype ------------------------------------------------
void openFile(int fileNum, string fileError, ifstream& file, char* argv[]);
void InitializeList(evenInfo *newEven, oddInfo *newOdd);
void ReadFile(ifstream& file, oddInfo *oddList, evenInfo *evenList);

// -------------------------------------------------------------


int main(int argc, char* argv[])
{
	ifstream file1;						//file input 1 from command line
	ifstream file2;						//file input 2 from command line
	evenInfo *newEven;						//node to be initialized for even link
	oddInfo *newOdd;						//node to be initialized for odd link
	bool evenEmpty;
	bool oddEmpty;
	int evenLength;
	int oddLength;

	newOdd = new oddInfo;
	newEven = new evenInfo;

	openFile(ONE, FILE_ONE_ERROR, file1, argv);
	openFile(TWO, FILE_TWO_ERROR, file2, argv);
	InitializeList(newEven, newOdd);

	evenEmpty = EmptyListEven(newEven);
	evenLength = ListLengthEven(newEven);

	oddEmpty = EmptyListOdd(newOdd);
	oddLength = ListLengthOdd(newOdd);

	ReadFile(file1, newOdd, newEven);

	cout << "Lists created using input file " << argv[1] << endl;
	DisplayListEven(newEven);
	DisplayListOdd(newOdd);

	system("PAUSE");
	return 0;
}


void openFile(int fileNum, string fileError, ifstream& file, char* argv[])
{
	file.open(argv[fileNum]);	     //first file to read
	string fileName;				//string for first file name

	//check the  file is valid
	while (!file){
		cout << "ERROR: Could not locate file" << endl;
		cout << fileError;
		cin >> fileName;
		file.open(fileName);
	}//end if
	//check the second file is valid

	return;

}

void InitializeList(evenInfo *newEven, oddInfo *newOdd)
{
	//Initializes odd node and check for memeory allocation
	newOdd = new oddInfo;
	if (newOdd == NULL){
		cout << "ERROR: Unable to allocate memory for queue." << endl;
	} //end if
	else {
		newOdd->top = NULL;
		newOdd->length = 0;
	} //end else
	//Initializes even node and check for memeory allocation
	newEven = new evenInfo;
	if (newEven == NULL){
		cout << "ERROR: Unable to allocate memory for queue." << endl;
	} //end if
	else {
		newEven->top = NULL;
		newEven->length = 0;
	} //end else

}


bool EmptyListEven(evenInfo *newStack)
{
	return(newStack->top == NULL);
}


bool EmptyListOdd(oddInfo *newStack)
{
	return(newStack->top == NULL);
}


int ListLengthEven(evenInfo *newNode)
{
	return(newNode->length);
}


int ListLengthOdd(oddInfo *newNode)
{
	return(newNode->length);
}


void DisplayListEven(evenInfo *newStack)
{
	int count = 0;

	cout << newStack->length << EVEN_COUNT;

	while (newStack->top != NULL) {
		if (count < 10){
			cout << setw(4) << newStack->top->integer;
			count++;
		}//end if
		else{
			cout << setw(4) << newStack->top->integer;
			count = 0;
		}//end else
	}//end while

}


void DisplayListOdd(oddInfo *newStack)
{
	int count = 0;

	cout << newStack->length << ODD_COUNT;

	while (newStack->top != NULL) {
		if (count < 10){
			cout << setw(4) << newStack->top->integer;
			count++;
		}//end if
		else{
			cout << setw(4) << newStack->top->integer;
			count = 0;
		}//end else
	}//end while

}

void ReadFile(ifstream& file, oddInfo *oddList, evenInfo *evenList)
{
	int readVal = 0;				//value read from file

	//primes file to be read
	file >> readVal;
	//while recieptFile still holds information keep reading
	while (file) {

		if ((readVal % 2) == 0){
			OrderedListInsertionEven(readVal, evenList);

		}//end if

		else {
			OrderedListInsertionOdd(readVal, oddList);

		}//end else

		file >> readVal;
	}//end while
}

void OrderedListInsertionEven(int val, evenInfo *newList)
{

	evenInfo *newNode;                // pointer to create each node

	newNode = new (nothrow)evenInfo; // allocate memory for 1st node 

	if (newNode == NULL)
		cout << "Memory allocation error -- cannot continue" << endl;
	else {
		newList->length++;
		newNode->top->prev = NULL;         // next and prev pointers are set to NULL
		newNode->top->next = NULL;         // since it is the only node

		newNode->top->integer = val;
		newNode->top->prev = NULL;
		newNode->top->next = newList->top;
		if (newList->top != NULL)
			newList->top->prev = newNode->top;
		newList = newNode;
	}
}

void OrderedListInsertionOdd(int val, oddInfo* newList)
{

	oddInfo *temp;                // pointer to create each node

	temp = new (nothrow)oddInfo; // allocate memory for 1st node 

	if (temp == NULL)
		cout << "Memory allocation error -- cannot continue" << endl;
	else {
		newList->length++;
		temp->top = NULL;         // next and prev pointers are set to NULL

		temp->top->integer = val;
		temp->top->prev = NULL;
		temp->top->next = newList->top;
		if (newList->top != NULL)
			newList->top->prev = temp->top;
		newList = temp;
	}
}
Line 265: You set temp->top to NULL.
Line 267-269: You now try to dereference this null pointer.
If i comment out line 265 i get an error Access violation writing location 0xCDCDCDD1 when i hit line 267.
So it looks like my issue was i was wanting to create a new node call oddLink *temp not oddInfo *temp. This allowed me to do what i was wanting to do. Now when i am setting the newList->top->prev to temp its giving me another allocation error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void OrderedListInsertionOdd(int val, oddInfo* newList)
{

	oddLink *temp;                // pointer to create each node

	temp = new oddLink; // allocate memory for 1st node 

	if (temp == NULL)
		cout << "Memory allocation error -- cannot continue" << endl;
	else {
		newList->length++;
		//temp->top = NULL;         // next and prev pointers are set to NULL

		temp->integer = val;
		temp->prev = NULL;
		temp->next = newList->top;
		if (newList->top != NULL){
			newList->top->prev = temp;
		}
		newList->top = temp;
	}
}


I should be able to set the pointer for newList->top->prev to temp but i am getting an allocation error. Doubly linked list are also new to me.
Topic archived. No new replies allowed.