0xC0000005: Access violation reading location 0x00000014.

This is a parchment of my code which is faulty. In this case I am trying to manipulate multiple linked lists. when I add a new Animal to the list, it gives an error "0xC0000005: Access violation reading location 0x00000014."
It points to the return statement of the following method.

int compare(const _Myt& _Right) const _NOEXCEPT
{ // compare [0, _Mysize) with _Right
return (compare(0, this->_Mysize, _Right._Myptr(), _Right.size()));
}
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;

struct Animal
{
	string name;
	int animal_rarity;
	Animal *a_next;
	Animal()
	{
		animal_rarity = -1;
	}
	void set_values(string str, int num)
	{
		name = str;
		animal_rarity = num;
	}
};

struct Region_Node
{
	string name;
	Region_Node *next;
	int region_rarity;
	Animal *a_head;
	Region_Node()
	{
		a_head = NULL;
		region_rarity = -1;
	}
	void set_values(string a)
	{
		name = a;
	}
	void Add_Animal(Animal* abc)
	{
		Animal  *ptr;
		if(!a_head)
		{
			a_head = abc;
		}
		else
		{
			ptr = a_head;
			while(ptr)
			{
				ptr = ptr->a_next;
			}
			ptr->a_next = abc;
		}
		cout << "\n\n\t\tAnimal Added Successfully. " << endl;
		return;
	}
	bool update_animal_rarity(string str,int a)
	{
		Animal *ptr;
		ptr = a_head;
		if(ptr && ptr->name == str)
		{
			ptr->animal_rarity = a;
			return true;
		}
		else 
		{
			ptr = ptr->a_next;
			while(ptr->a_next && ptr->name != str)
			{
				ptr = ptr->a_next;
			}
			if(ptr->name == str)
			{
				ptr->animal_rarity = a;
				return true;
			}
			return false;
		}
	}
	void animal_list(string str)
	{
		Animal *ptr;
		ptr = a_head;
		if(ptr && ptr->animal_rarity > 0 && ptr->name != "\0")
		{
			cout << "\n\t\tName of Region : " << str << endl;
			cout << "\n\t\tAnimal Name : " << ptr->name <<endl;
			ptr = ptr->a_next;
			while(ptr && ptr->animal_rarity >0 && ptr->name != "\0")
			{
				cout << "\n\t\tAnimal Name : " << ptr->name <<endl;
				ptr = ptr->a_next;
			}
		}
	}
	void search_by_name(string r_name, string str)
	{
		Animal *ptr;
		ptr = a_head;
		if(!ptr)
		{
			return;
		}
		else
		{
			if(ptr->name == str)
			{
				cout << "\tRegion : " << r_name << " \t\tAnimal : " << ptr->name <<endl;
				return;
			}
			else 
			{
				ptr = ptr->a_next;
				while(ptr->a_next && ptr->name != str)
				{
					ptr = ptr->a_next;
				}
				if(ptr->name == str)
				{
					cout << "\tRegion : " << r_name << " \t\tAnimal : " << ptr->name <<endl;
					return;
				}
			}
		}
	}
	void search_by_rarity(string r_name, int b)
	{
		Animal *ptr;
		ptr = a_head;
		if(!ptr)
		{
			return;
		}
		else
		{
			if(ptr->animal_rarity == b)
			{
				cout << "\tRegion : " << r_name << " \t\tAnimal : " << ptr->name <<endl;
				return;
			}
			else 
			{
				ptr = ptr->a_next;
				while(ptr->a_next && ptr->animal_rarity != b)
				{
					ptr = ptr->a_next;
				}
				if(ptr->animal_rarity == b)
				{
					cout << "\tRegion : " << r_name << " \t\tAnimal : " << ptr->name <<endl;
					return;
				}
			}
		}
	}
	bool check_animal_name(string a)
	{
		Animal *ptr;
		ptr = a_head;
		if(!ptr)
		{
			return true;
		}
		else
		{
			while(ptr && ptr->name != a)
			{
				ptr = ptr->a_next;
			}
			if(ptr->name == a)
			{
				return false;
			}
			return true;
		}
	}
	~Region_Node()
	{
		Animal *ptr;
		ptr = a_head;
		while(ptr)
		{
			a_head = ptr->a_next;
			delete ptr;
			ptr = a_head;
		}
	}
};
class Region
{
	Region_Node *head;
public:
	Region()
	{
		head = NULL;
	}
	string get_region_name()
	{
		string str;
		fflush(stdin);
		cout << "\n\n\t\tEnter the Name of the Region : ";
		fflush(stdin);
		getline(cin,str);
		return str;
	}
	void Add_Region()
	{
		Region_Node *newNode , *ptr;
		newNode = new Region_Node;
		newNode->set_values(this->get_region_name());
		newNode->next = NULL;
		if(!head)
		{
			head = newNode;
		}
		else
		{
			ptr = head;
			while(ptr->next)
			{
				ptr = ptr->next;
			}
			ptr->next = newNode;
		}
		cout << "\n\n\t\tRegion Added Successfully ." << endl;
		return;
	}
	string get_animal_name()
	{
		string str;
		fflush(stdin);
		cout << "\n\n\t\tEnter the Name of the Animal : ";
		fflush(stdin);
		getline(cin,str);
		return str;
	}
	int get_animal_rarity()
	{
		int num;
		cout << "\n\n\t\tEnter the Rarity of the Animal : ";
		cin >> num;
		return num;
	}
	Animal* get_Animal()
	{
		Animal *newAnimal;
		newAnimal = new Animal;
		newAnimal->set_values(this->get_animal_name(),get_animal_rarity());
		newAnimal->a_next = NULL;
		return newAnimal;
	}
	void Add_Animal()
	{
		Region_Node *ptr;
		Animal *newAnimal = this->get_Animal();
		ptr = head;
		string name = this->get_region_name();
		if(head)
		{
			if(ptr->region_rarity == -1 || ptr->name == name)
			{
				ptr->Add_Animal(newAnimal);
				this->update_region_rarity(ptr->name);
			}
			else
			{
				ptr = ptr->next;
position:
			{
				while(ptr->next && ptr->region_rarity != -1 && ptr->name != name)
				 {
					ptr = ptr->next;
				 }
				if(ptr->region_rarity == -1 || ptr->name == name)
				 {
					ptr->Add_Animal(newAnimal);
					this->update_region_rarity(ptr->name);
				 }
			}
				if(ptr->next)
				{
					ptr = ptr->next;
					goto position;
				}
				else if(!ptr->next && ptr->next->region_rarity == -1 )
				{
					ptr->next->Add_Animal(newAnimal);
				}
			}
		}

	}
	void update_region_rarity(string str)
	{
		Region_Node *ptr;
		ptr = head;
		if(ptr && ptr->name == str)
		{
			ptr->region_rarity = 10;
			return;
		}
		else 
		{
			while(ptr->next && ptr->name != str)
			{
				ptr = ptr->next;
			}
			if(ptr->name == str)
			{
				ptr->region_rarity = 10;
				return;
			}
		}
	}
	void update_animal_rarity(string str,string str1,int a)
	{
		Region_Node *ptr;
		ptr = head;
		if(ptr && ptr->name == str)
		{
			if(ptr->update_animal_rarity(str1,a))
			{
				cout << "\n\t\tRarity Found And Updated. " << endl;
				this->update_region_rarity(ptr->name);
				return;
			}
		}
		else 
		{
			while(ptr->next && ptr->name != str)
			{
				ptr = ptr->next;
			}
			if(ptr->name == str)
			{
				if(ptr->update_animal_rarity(str1,a))
			    {
				   cout << "\n\t\tRarity Found And Updated. " << endl;
				   this->update_region_rarity(ptr->name);
				   return;
			    }	
				else
			    {
				   cout << "\n\t\tRarity Not Found " << endl;
				   return;
			    }
			}
			return;
		}
	}
	void region_list()
	{
		Region_Node *ptr;
		ptr = head;
		if(head && ptr->name != "\0")
		{
			cout <<  "\n\n\tName of Region : " << ptr->name << endl;
			ptr = ptr->next;
			while(ptr && ptr->name != "\0")
			{
				cout <<  "\n\n\tName of Region : " << ptr->name << endl;
				ptr = ptr->next;
			}
		}
		else 
		{
			cout << "\n\n\t\tRegion List is empty. "<<endl;
			return;
		}
	}
	void animal_list()
	{
		Region_Node *ptr;
		ptr = head;
		if(head)
		{
			ptr->animal_list(ptr->name);
			ptr = ptr->next;
			while(ptr)
			{
				ptr->animal_list(ptr->name);
				ptr = ptr->next;
			}
		}
	}
	void name_search_list(string name)
	{
		Region_Node *ptr;
		ptr = head;
		if(!head)
		{
			cout << "\n\t\tRegion List is Empty. " << endl;
			return;
		}
		else
		{
			while(ptr)
			{
				ptr->search_by_name(ptr->name,name);
				ptr = ptr->next;
			}
		}
	}
	void rarity_search_list(int a)
	{
		Region_Node *ptr;
		ptr = head;
		if(!head)
		{
			cout << "\n\t\tRegion List is Empty. " << endl;
			return;
		}
		else
		{
			while(ptr)
			{
				ptr->search_by_rarity(ptr->name,a);
				ptr = ptr->next;
			}
		}
	}
	~Region()
	{
		Region_Node *ptr;
		ptr = head;
		while(ptr)
		{
			head = ptr->next;
			delete ptr;
			ptr = head;
		}
	}
};

I need urgent help with this. Help would be much appreciated Thank you !
Last edited on
1. A debugger will find the problem quickly.
2. Access violation means you are trying to access a variable that has not been allocated.
I'm not sure where you're "adding" an animal that is causing this error, as the indicated code is not present in what you posted. Adding an animal through Region::Add_Animal doesn't appear to do anything but leak memory.

If you're looking at a stack trace, you need to follow the trace down until you reach your own code. If you're not looking at a stack trace, you should be.

>	Assignment _1.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::compare(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & _Right) Line 2117	C++


The Call Stack of my code prints this when the exception occurs, by the looks of it , It seems that there is a problem with the "string" allocation, but i don't know how to get rid of this error.
If you're looking at a stack trace, you need to follow the trace down until you reach your own code.

For instance, I wouldn't be surprised to learn it originated on line 304 of the code you posted in the OP. Can you see why?
Yes, I get it now. Thank you for all your help "cire". Really appreciate it.
Topic archived. No new replies allowed.