Weird Error at runtime ( 0xC0000005: Access violation reading location 0x00000000.)

So I've been struggling to understand why I'm getting an error like this and I'm really not sure how to solve it or why its happening, can anyone help?

Error is:

0xC0000005: Access violation reading location 0x00000000.

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
#include "stdafx.h"
#include <iostream>
#include <memory>
#include <deque>
#include <algorithm>
using namespace std;


struct coords
{
	int x;
	int y;
};

void Raw(coords* t)
{
	cout << t->x << " " << t->y << endl;
}


bool find(coords* t, const deque<unique_ptr<coords>>& openList)
{

	for (auto p = openList.begin(); p != openList.end(); p++)
	{
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
		if (t->x == (*p)->x && t->y == (*p)->y) //Error is on this line apperently
////////////////////////////////////////////////////////////////////////////////////////////////////////		
                 {
			cout << "The coords searched for are in the open list" << endl;
			return true;
		}
	}
	return false;
}

void bfs() /////Breadth-First Search
{
	deque <unique_ptr < coords > > openList;
	deque <unique_ptr < coords > > closedList;	

	unique_ptr <coords> start(new coords);
	unique_ptr <coords> end(new coords);
	start->x = 4;
	start->y = 4;
	end -> x = 8;
	end -> y = 8;

	openList.push_back(move(start));//The first element of the OPenlist
	unique_ptr < coords > current(new coords);
		unique_ptr <coords> next(new coords);
	while (current != end)
	{	
		
	//	openList.pop_front();
		current = move(openList.front());
		

		if (openList.empty() )
		{
			cout << "Empty" << endl;
			//break;
		}
		if (current == end)
		{
			cout << "Success" << endl;
			break;
		}
		//NORTH//////////////////////////////////
		next->x = current->x;
		next->y = current->y+1;
		if (find(next.get(), openList) == true || find(next.get(), closedList) == true)
		{
			closedList.push_back(move(current));
			openList.push_back(move(next));
		}
		///////////////////////////////////////// 
Last edited on
Note that move assignment does not remove the unique_ptr from the container. It simply sets it equal to nullptr.

1
2
current = move(openList.front());
// openList.front() == nullptr 
I tried your find function and it seemed to work. As an aside, avoid function names that are same as standard library ones:

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
#include <iostream>
#include <memory>
#include <deque>
#include <algorithm>//edit: std::move is defined in header <utility>
using namespace std;//to be avoided as well

struct coords
{
	int x;
	int y;
	coords (){}
	coords (const int& temp_x, const int& temp_y) : x(temp_x), y(temp_y){}
};

void Raw(coords* t)
{
	cout << t->x << " " << t->y << endl;
}
bool find(coords* t, const deque<unique_ptr<coords>>& openList)
{

	for (auto p = openList.begin(); p != openList.end(); p++)
	{
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
		if (t->x == (*p)->x && t->y == (*p)->y) //Error is on this line apperently
////////////////////////////////////////////////////////////////////////////////////////////////////////
                 {
			cout << "The coords searched for are in the open list" << endl;
			return true;
		}
	}
	return false;
}

void bfs() /////Breadth-First Search
{
	deque <unique_ptr < coords > > openList;
	deque <unique_ptr < coords > > closedList;

	unique_ptr <coords> start(new coords);
	unique_ptr <coords> end(new coords);
	start->x = 4;
	start->y = 4;
	end -> x = 8;
	end -> y = 8;

	openList.push_back(move(start));//The first element of the OPenlist
	unique_ptr < coords > current(new coords);
		unique_ptr <coords> next(new coords);
	while (current != end)
	{

	//	openList.pop_front();
		current = move(openList.front());


		if (openList.empty() )
		{
			cout << "Empty" << endl;
			//break;
		}
		if (current == end)
		{
			cout << "Success" << endl;
			break;
		}
		//NORTH//////////////////////////////////
		next->x = current->x;
		next->y = current->y+1;
		if (find(next.get(), openList) == true || find(next.get(), closedList) == true)
		{
			closedList.push_back(move(current));
			openList.push_back(move(next));
		}
	}
}
int main()
{
    coords* t = new coords(1,2);
    unique_ptr<coords> s1 (new coords(3, 4));
    unique_ptr<coords> s2 (new coords(1, 2));

    deque<unique_ptr<coords>> coords_ptr;
    coords_ptr.push_back(std::move(s1));
    coords_ptr.push_back(std::move(s2));

    find (t, coords_ptr);

    delete t;
}
		///////////////////////////////////////// 


The function bfs() is poorly constructed, apart from lots of magic numbers let's go through the logic:
line 49: first element of openList is unique_ptr<coords> start which is moved to unique_ptr<coords> current on line 56. As Peter points out this does not actually remove start from openList but sets it == nullptr. So the following condition if(openList.empty()), line 59, will always be false.
Then the expression on line 64, if (current == end) will never be true because current points to (4, 4) (from start) and end points to (8, 8) (from lines 46, 47).
Line 72, both expressions of the or statement will always be false since (a) openList now just has the nullptr and (b) closedList doesn't have anything! Instead of local variables openList, closedList you might have to pass these deque<unique_ptr<coords>> to the function as parameters


Last edited on
@ SCB3

Consider using a range based for loop rather than begin and end iterators:

1
2
3
for (const auto& Element : openList ) {
       // use Element, one of the objects in the container
}


http://en.cppreference.com/w/cpp/language/range-for

I don't see why you need to use pointers at all, you can avoid double indirection, std::move, unique_ptr. The STL works very well on it's own - without having to use all these.

If you take these points into consideration, then the code will be less error prone and easier to read.

Also, ideally one should put their own code into it's own namespace/s.

Avoid using new like you do on line 79.
Last edited on
Topic archived. No new replies allowed.