Hi, I have this program due tomw night and there is a problem. When I run it on visual c, it works just fine, but when running in unix, i get a seg fault.
In the recursive function ncubed (), I call the function two_array() at least 5 times. I put a printf in the two_array() function and when running the program, MY DEBUG TOOL prints out only 4 times....and then i get a seg fault...if there is anything obviously wrong in the code below...please let me know asap!
short **two_array(int size, short ***matrix)
{
int x;
(*matrix)=(short **)malloc(size*sizeof(short*));
for(x=0;x<size;x++)
(*matrix)[x] = (short*)malloc(size*sizeof(short));
printf("MY DEBUG TOOL");
return *matrix;
}
short **ncubed(short ***matrix1, short ***matrix2, int dim,int thresh)
{
int b,j,k;
int i=dim/2;
short **matrix3=two_array(dim,&matrix3);
short **A=*matrix1;
short **E=*matrix2;
short **C=&((*matrix1)[i]);
short **G=&((*matrix2)[i]);
short **B=two_array(i,&B);
short **F=two_array(i,&F);
short **D=two_array(i,&D);
short **H=two_array(i,&H);
for(j=0;j<i;j++)
{
k=i;
for(b=0;b<i;b++)
B[j][b]=(*matrix1)[j][k++];
}
for(j=0;j<i;j++)
{
k=i;
for(b=0;b<i;b++)
F[j][b]=(*matrix2)[j][k++];
}
for(j=0;j<dim/2;j++)
{
k=dim/2;
for(b=0;b<dim/2;b++)
D[j][b]=(*matrix1)[i][k++];
i++;
}
i=dim/2;
for(j=0;j<dim/2;j++)
{
k=dim/2;
for(b=0;b<dim/2;b++)
H[j][b]=(*matrix2)[i][k++];
i++;
}
i=dim/2;
if(i==thresh)
{
short **AE=mult(i,&A,&E);
short **BG=mult(i,&B,&G);
short **CE=mult(i,&C,&E);
short **DG=mult(i,&D,&G);
short **AF=mult(i,&A,&F);
short **BH=mult(i,&B,&H);
short **CF=mult(i,&C,&F);
short **DH=mult(i,&D,&H);
matrix3=combine1(dim,&AE,&BG,&AF,&BH,&CE,&DG,&CF,& DH);
free(AE);
free(BG);
free(CE);
free(DG);
free(AF);
free(BH);
free(CF);
free(DH);
}
else
{
short **AE=ncubed(&A,&E, i ,thresh);
short **BG=ncubed(&B,&G, i ,thresh);
short **CE=ncubed(&C,&E, i ,thresh);
short **DG=ncubed(&D,&G, i ,thresh);
short **AF=ncubed(&A,&F, i ,thresh);
short **BH=ncubed(&B,&H, i ,thresh);
short **CF=ncubed(&C,&F, i ,thresh);
short **DH=ncubed(&D,&H, i ,thresh);
matrix3=combine1(dim,&AE,&BG,&AF,&BH,&CE,&DG,&CF,& DH);
free(AE);
free(BG);
free(CE);
free(DG);
free(AF);
free(BH);
free(CF);
free(DH);
}
free(B);
free(F);
free(D);
free(H);
return matrix3;
}