I am new to C++ and trying to create my first Octree structure.
Common way to store children is simply to store 8 pointers in each node.
like so:
class Octant
{
Octant *child0;
Octant *child1;
Octant *child2;
//...
}
This means each node will contain 8 pointers, witch is 64 bytes per node.
And also each leaf will store 8 null pointers.
To avoid memory wasting I want to store only one pointer to the first child
and allocate the array only during subdividing a leaf.
I tried this:
Octant *child[8]; // actually the same as 8 pointers + 1 pointer to an array
Octant child[8]; // One pointer but I have to allocate children in each Octant memory
so I did something like this:
class Octant
{
private:
Octant *child; // Store only one pointer in each Octant
public:
Octant();
void subdivide();
void doStuff(int i){ std::cout << i << " Child exists and do some stuff \n"; }
//...
};
Octant::Octant() {
child = nullptr; // leaves have no allocated children
//...
}
void Octant::subdivide() {
child = new Octant[8]; // I can allocate it wherever I want
child[0].doStuff(0);
child[1].doStuff(1);
child[2].doStuff(2);
child[3].doStuff(3);
//...
}
It works fine, but there is a little problem during debugging.
My code "thinks" that the child is a pointer to one Octant not an array
For example if i do something like sizeof(child) it will return sizeof(pointer)
(8 bytes)
So my question is it ok to use pointers like I did?
Or is there another "proper" way to do such stuff in C++?
Is there a way to convert a pointer to pointer[8] and does it make any sense?