"Access violation writing location 0x00000000." - Object pointed by a pointer, returned by a function, cannot be accessed.

A function -> int_to_coordinate(int, int id_array[5][5]) returns a pointer to an object in an array, the object can be accessed through the pointer in this function, but cannot be accessed by any function that uses the returned pointer. By "cannot be accessed" I mean the compiler catches an unhandled exception and gives that "Access violation..." error.

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
class Grid_info
{
public:
    Grid_info::Grid_info();          
    Grid_info::Grid_info(int);    
                                     
    int item[5][5];       
    int direction[5][5];
	
private:

//Other functions...


        vector<int>sample_vct;

        bool vectors_to_arrays(vector<int> *v, char); //calls int_to_coordinate(int, int[5][5]) and uses the pointer returned 
        int* int_to_coordinate(int, int[5][5]);  //returns pointer to object in array (item[5][5])
	
}; 

bool Grid_info::vectors_to_arrays(vector<int> *v, char frog_bug)  
{
	int fb=-1;
	if (frog_bug=='f') fb=1;
	else if (frog_bug=='b') fb=2;
	else sys_error(...);

	for (int a=0; (unsigned)a<(v->size()); a++)
	{
		int* position=int_to_coordinate(v->at(a),item);  //passes an integer and an array _ assigns the returned pointer to int* position
                
                *position=fb;  //>>> gives "Access violation..." error, tested diff functions, can't access the object pointed by the pointer returned by int_to_coordinate(int, ...)
	}

        return true;
}

int* Grid_info::int_to_coordinate(int pos, int id_array[5][5])   //returns a pointer to one of the objects in the array passed as argument
{
        int* pos_p=0; 
	int counter=1;

        for(int a=0; a<5; a++)
	{
		for(int b=0; b<5; b++)
		{
			
			
			if (counter==pos) 
			{
				pos_p=&id_array[a][b];
			    
	                        //the object pointed by the pointer pos_p can be accessed at this point (*pos_p=int)_tested		        
                                
                                if (pos_p=0) sys_error(...);
				return pos_p;
			}
		    counter++;
		}
	}

	sys_error(...);

}


maybe place braces around &id_array[a][b]
like so: &(id_array[a][b])
otherwise you get the address of id_array instead of id_array[a][b]...
tried it, no difference... pos_p actually gets the correct address, i can access it from this function perfectly fine, i just can't access it from any other function that uses the pointer (pos_p) returned by int* Grid_info::int_to_coordinate(...)
if (pos_p=0) sys_error(...);
You're setting pos_p to zero here, right before you return it, every time. I suspect you meant:

if (pos_p==0) sys_error(...);
Last edited on
yes, i did mean (pos_p==0)... Sigh, i've spent nearly 8h looking for this bug... no wonder it worked when i did
1
2
3
4
5
6
7
8
if (counter==pos) 
 {
	pos_p=&id_array[a][b];
        debug_pointer=pos_p;
			    
	if (pos_p=0) sys_error(...);
	return pos_p;
}

for a global pointer (debug_pointer) and skipped using the returned value.

Thank you very much Moschops, you've been most helpful; and subsequently this forum has proved to be much more helpful than I expected.
Topic archived. No new replies allowed.