Thread: seg fault on unix platform

  1. #1
    Unregistered
    Guest

    seg fault on unix platform

    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;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I think we need to see the function that calls ncubed. How are matrix1 and matrix2 allocated back in the calling function?

  3. #3
    Unregistered
    Guest
    Here is the main() function that calls ncubed()...

    int main(int argc, char **argv)
    {
    int i,j;
    int * parsedArgs;
    int useStrassen;
    int threshold;
    int iterations;
    int maxThreads;
    char * filenameA;
    char * filenameB;
    char * filenameC;
    FILE *fA;
    FILE *fB;
    FILE *fC;
    int dim=1;
    short temp;
    short **matrixA;
    short **matrixB;
    short **matrixC;
    parsedArgs = parseArgs(argc,argv);
    useStrassen = parsedArgs[0];
    iterations = parsedArgs[1];
    threshold = parsedArgs[2];
    maxThreads = parsedArgs[3];
    filenameA = argv[2];
    filenameB = argv[3];
    filenameC = argv[4];

    fA=fopen(filenameA, "rb");
    fB=fopen(filenameB, "rb");

    /* READ IN INPUT MATRICES HERE */

    while((temp=(fgetc(fA)))!='\n')
    {
    if (temp==' ')
    dim++;
    }
    matrixA=two_array(dim, &matrixA);
    matrixB=two_array(dim, &matrixB);
    matrixC=two_array(dim, &matrixC);
    matrixA=store_matrix(fA, dim, &matrixA);
    matrixB=store_matrix(fB, dim, &matrixB);

    fprintf(stderr,"About to start\n");
    for(i=0;i<iterations;i++)
    {
    fprintf(stderr,"Iteration %d ...",i);

    /* PERFORM MULTIPLICATION HERE */

    if(useStrassen)
    matrixC=strassen(&matrixA, &matrixB, dim,threshold);
    else
    matrixC=ncubed(&matrixA,&matrixB,dim,threshold);
    }

    fprintf(stderr," Done!\n");
    fprintf(stderr,"Done running\n");

    /* WRITE OUTPUT MATRIX HERE */

    fC=fopen(filenameC, "w");

    for(i=0;i<dim;i++)
    {
    for(j=0;j<dim;j++)
    {
    fprintf(fC, "%d", matrixC[i][j]);
    if(j!=dim-1)
    fprintf(fC, " ", matrixC[i][j]);
    }
    fprintf(fC, "%\n");
    }

    /* DON'T FORGET TO FREE WHATEVER YOU MALLOCED */

    fclose(fA);
    fclose(fB);
    fclose(fC);
    free(matrixA);
    free(matrixB);
    free(matrixC);
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    > filenameA = argv[2];
    You didn't allocate memory for the pointers in this function, because of that you try to place data into an area of memory that can't handle it. This is your problem, and why you get a segmentation fault. Always allocate the required memory to your pointers (if applicable) before working with them.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM