I am relatively new to C++ and am having problems understanding struct.
I have a struct declared as follow
struct MyNode {
int level;
int index;
MyNode children[4];
}
However the code fails to compile and reports error C2148: total size of array must not exceed 0x7fffffff bytes.
But the following code compiles
struct MyNode {
int level;
int index;
MyNode* children;
}
Can i code MyNode as in the first example or is there something that I am missing.
Thanks!
This fails to compile, because the compiler needs to know the size of each type.
So, what’s
sizeof(MyNode)? It’ssizeof(int) * 2 + sizeof(MyNode): The recursion makes the size impossible to figure out.You need a pointer, as in your second example. Why does this works ? Because
sizeof(MyNode*)is known : it’s the size of an address on the target platform.