I implemented this function to create a tree of dept d and with branch factor b:
void create(node *n, int b, int d){
int cont,i;
if(d>0){
n->children = (node *) malloc(b*sizeof(node));
if(!n->children){
printf("\n\nMemory couldn't be allocated\n");
getchar();
return;
}
n->alpha = -100;
for(i=0;i<b;i++){
create((n->children+i*sizeof(node)), b, d-1);
}
}
else if(d==0){
if(n){
n->alpha = rand()%9 + 1;
printf("%d ",n->alpha);
}
}
It works fine for d<6 and b<6, but when b =6 and d=6, or bigger, it gives me a segmentation fault.
But when I change the line create((n->children+i*sizeof(node)), b, d-1); for the line create((&n->children[i]), b, d-1);, it works perfectly for any d and b, as far as I have tested. But the two lines truly are the same!!! just the address of the children struct.... So, does anybody know why is it happening? Doesn't malloc allocates one contiguous block of memory?
this thing really made me confused!! pls, help!
Thanks =) ,
Ingrid