Hi everybody!
I am once again having some trouble with re-allocating memory. The basis of the following is this struct:
Code:
typedef struct _MyStruct {
struct _MyStruct *Father;
struct _MyStruct *Sons[2];
} MyStruct;
typedef struct _MyStruct2 {
MyStruct *Structs;
} MyStruct2;
So I have one MyStruct2 in which there is an array of MyStructs in which there are pointers to other MyStructs (a hierarchy).
These variables are important:
Code:
long SIZE;
long INCREASE;
I assign memory and increase memory with the following functions:
Code:
int NEW(MyStruct2 **STRUCT2) {
*STRUCT2=(MyStruct2 *)malloc(sizeof(MyStruct2));
(*STRUCT2)->Structs=(MyStruct *)malloc(SIZE*sizeof(MyStruct));
}
int ENLARGE(MyStruct2 **STRUCT2) {
SIZE=SIZE+INCREASE;
(*STRUCT2)->Structs=(MyStruct *)realloc((*STRUCT2)->Structs,SIZE*sizeof(MyStruct));
}
My main procedure looks like this:
Code:
int DO(MyStruct2 **STRUCT2, MyStruct *STRUCT) {
// Fill in father and sons
// increase counter for used STRUCTS
// if counter large enough:
ENLARGE(STRUCT2);
// and then recursively:
for (a=0; a<2; a++) DO(STRUCT2,STRUCT->Sons[d];
}
int WHATEVER(MyStruct2 **STRUCT2) {
NEW(STRUCT2);
DO(STRUCT2,(*STRUCT2)->Structs);
}
The first function that is called (out of some other code) is WHATEVER.
I know that it may look weird and maybe confusing, but this is the simplest I could do out of my code. I only included the (as I see it) relevant parts for my problem.
As it seems, the program works fine until the second call of ENLARGE. After the second call of ENLARGE, there is a segmentation fault (the pointers do not point to the right structures any more???).
It would be so great if anybody could help me with this problem!
Thanks a lot!