Pointer/Octree/Access-violation Problem

I have this class that is going to be an Octree class. I am using an array of pointers to reference each 'node' beyond the current node, my issue is that when I initiate the first node I get Access violation errors.


"First-chance exception at 0x010212b2 in Finity.exe: 0xC0000005: Access violation writing location 0x00000000.
Unhandled exception at 0x010212b2 in Finity.exe: 0xC0000005: Access violation writing location 0x00000000.
The program '[1820] Finity.exe: Native' has exited with code -1073741510 (0xc000013a).
"




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
#include <GL/glew.h>
#include <GL/gl.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/glu.h>


#ifndef OCTREE_H
#define OCTREE_H



typedef int TYPE;
#define NULL 0

#define TOP_FRONT_LEFT			0
#define TOP_FRONT_RIGHT			1
#define TOP_BACK_LEFT			2
#define TOP_BACK_RIGHT			3
#define BOTTOM_FRONT_LEFT		4
#define BOTTOM_FRONT_RIGHT		5
#define BOTTOM_BACK_LEFT		6
#define BOTTOM_BACK_RIGHT		7

struct Node
{
	TYPE data;

	Node* children[8];

};

class Octree
{
public:
        //HERE I GET ACCESS VIOLATION ERRORS
	void initNode(struct Node *head, TYPE value)
	{
		head->data = value;
		for(int i=0; i<8; i++)
			head->children[i]=NULL;

	}


	void drawRoot(Node* root, float x, float y, float z, float width)
	{
		if(root==NULL)
			return;

		for(int i=0;i<8;i++)
		{
			if(root->children[i]!=NULL)
			{
				float x2;
				float y2;
				float z2;

				switch(i)
				{
				case TOP_FRONT_LEFT:  x2=x-width/4; y2=y+width/4; z2=z-width/4; break;
				case TOP_FRONT_RIGHT: x2=x+width/4; y2=y+width/4; z2=z-width/4; break;
				case TOP_BACK_LEFT:   x2=x-width/4; y2=y+width/4; z2=z+width/4; break;
				case TOP_BACK_RIGHT: x2=x+width/4; y2=y+width/4; z2=z+width/4; break;
				case BOTTOM_FRONT_LEFT:  x2=x-width/4; y2=y-width/4; z2=z-width/4; break;
				case BOTTOM_FRONT_RIGHT: x2=x+width/4; y2=y-width/4; z2=z-width/4; break;
				case BOTTOM_BACK_LEFT:   x2=x-width/4; y2=y-width/4; z2=z+width/4; break;
				case BOTTOM_BACK_RIGHT: x2=x+width/4; y2=y-width/4; z2=z+width/4; break;
				
				}


				drawRoot(root->children[i],x2,y2,z2,width/2);
				return;
			}

			else
			{
				glBegin(GL_QUADS);

                                        //TODO:

					glVertex3f(x-width/2,y-width/2,z-width/2);
					glVertex3f(x-width/2,y+width/2,z-width/2);
					glVertex3f(x+width/2,y+width/2,z-width/2);
					glVertex3f(x+width/2,y-width/2,z-width/2);
				glEnd();

			}
		}
	}
	Node* root;
};
#endif 


If anyone could tell me why this is not working that would be wonderful. Cheers.

I'd say that head doesn't point to a valid object.

Your class Octree doesn't have a constructor where you're supposed to set root to NULL.

I'd suggest that you change Node to a class. Provide a constructor and move the stuff from initNode() to that constructor.
lol, after spending hours upon hours I realized I didn't initialize root AFTER I posted this but thanks for the advice
I didn't initialize root AFTER I posted this
That's no surprise. The process to find the correct question often bears the answer as well
Topic archived. No new replies allowed.