Here my example code which works fine:
//define struct
struct myStruct {
char *arrChar1;
char *arrChar2;
}
// code in Main
struct myStruct *structDeep1 = malloc( sizeof( struct myStruct *) );
structDeep1->arrChar1 = NULL;
structDeep1->arrChar2 = NULL;
structDeep1->arrChar1 = malloc( sizeof(char ) * 2); //2 char
if( structDeep1->arrChar1 == NULL) puts("trace error");
else puts("trace ok")
//stdout -> trace OK
No problem.
Now here my example with strange error:
// define a second struct
struct myStructDeep2{
struct myStruct *structDeep1;
}
// next code in Main
struct myStructDeep2 structDeep2 = malloc( sizeof( struct myStructDeep2*) );
structDeep2->structDeep1 = malloc( sizeof( struct myStruct *) );
structDeep2->structDeep1->arrChar1 = NULL;
structDeep2->structDeep1->arrChar2 = NULL;
structDeep2->structDeep1->arrChar1 = malloc( sizeof(char ) * 2); //2 char
if( structDeep2->structDeep1->arrChar1 == NULL) puts("trace error");
else puts("trace ok")
//stdout -> trace error
It's seem the malloc function write/crush in the second pointer. I don't understand where is the problem in my code, it's very strange.
sizeof( structDeep2 )??