I have a recursive, tree-like struct (TestStruct
in the example code below) and basically a generator function which takes one of these and outputs the next one. My first attempt was similar to the code below (which does NOT work). I realize now that the sub
variable only exists within the scope of gen_next
, so the pointers (sub0
,sub1
) in the parent TestStructs
no longer point to anything.
I do not have very much experience with C++ (or in general languages with this level of memory control), and I don't really know the best way to work around this. I thought of storing everything in global variables, but that seems pretty messy, and I feel like I'm missing a much simpler way to accomplish this.
#include <stdio.h>
struct TestStruct
{
int type;
struct TestStruct * sub0;
struct TestStruct * sub1;
TestStruct()
{
type = 0;
sub0 = NULL;
sub1 = NULL;
}
TestStruct(int type_init, TestStruct * sub0_init, TestStruct * sub1_init)
{
type = type_init;
sub0 = sub0_init;
sub1 = sub1_init;
}
};
TestStruct gen_next(int size, TestStruct last)
{
TestStruct empty;
TestStruct sub;
if (size==0)
{
if (last.type == 1)
return TestStruct();
if (last.type == 0)
return TestStruct(1,NULL,NULL);
}
if (last.type == 0)
{
sub = gen_next(size-1,empty);
return TestStruct(2,&sub,NULL); // bad code; sub will no longer exist!
}
if (last.type == 2)
{
sub = gen_next(size-1,*last.sub0);
if (sub.type == 0)
sub = gen_next(size-1,empty);
return TestStruct(3,NULL,&sub);
return TestStruct(2,&sub,NULL);
}
if (last.type == 3)
{
sub = gen_next(size-1,*last.sub1);
if (sub.type == 0)
return TestStruct();
return TestStruct(3,NULL,&sub);
}
throw;
}
void print_struct(TestStruct test_struct)
{
switch (test_struct.type)
{
case 0: printf("NONE");
break;
case 1: printf("END");
break;
case 2: printf("LEFT("); print_struct(*test_struct.sub0); printf(")");
break;
case 3: printf("RIGHT("); print_struct(*test_struct.sub1); printf(")");
break;
default: printf("INVALID:%d",test_struct.type);
}
}
int main()
{
TestStruct test;
test = gen_next(3,test);
print_struct(test);
printf("\n");
do {
test = gen_next(3,test);
print_struct(test);
} while (test.type != 0);
return 0;
}