Malloc returning segmentation fault

Discussion in 'C Programming' started by Dawn Minnis, Mar 15, 2005.

  1. Dawn Minnis

    Dawn Minnis Guest

    Hey guys - this code when called with parameters:

    driver.o n n 12 12 12 12 12 12 2.6 3.2

    is kicking back a segmentation fault.

    I've read the rest of the postings but am still confused. Can someone
    take a look and tell me how to fix it - please dont be like the guy I
    spoke to today and tell me that I am not allocating the memory
    correctly and then walk off. Please, if possible provide me with
    solutions or at least suggestions to try out.

    Thanks

    Dawn

    /*driver: used to handle arguments*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <sys/time.h>


    /* define the external functions */
    extern void callhelp();
    //extern void getparams(int argc, char *argv[], double params[]);
    extern void buildMatrix(double *arrayX, int dim1, int dim2);
    extern void myMxM(char *transA, char *transB, int *m, int *n, int *k,
    double *alpha, double *arrayA, int *lda, \
    double *arrayB, int *ldb, double *beta, double *arrayC, int *ldc);

    extern void dgemm_(char *transA, char *transB, int *m, int *n, int *k,
    double *alpha, double *arrayA, int *lda, \
    double *arrayB, int *ldb, double *beta, double *arrayC, int *ldc);

    extern void lsame_(char*,char*);
    extern void xerbla_(char*,int*);

    int main(int argc, char *argv[])
    {
    int c, flag, m, n, k, lda, ldb, ldc, d, e, f, arrayDim=0,
    tempArrayDim=0;
    int *ptrM, *ptrN, *ptrK, *ptrLda, *ptrLdb, *ptrLdc;
    char transA, transB;
    double alpha, beta;
    double *arrayA, *arrayB, *arrayC, *tempArrayC, *ptrAlpha, *ptrBeta;
    char *endptr, *ptrTransA, *ptrTransB;

    struct timeval start, stop;

    while (--argc > 0 && (*++argv)[0] == '-')
    {
    while (c = *++argv[0])
    { flag = 0;
    switch (c)
    {
    case 'h':
    /*printf("\ncall help file");*/
    callhelp();
    argc = 0;
    flag = 1;
    break;
    default:
    printf("find: illegal option %c\n", c);
    argc = 0;
    flag = 1;
    break;
    }
    }//end inner while
    }//end outer while


    //Check for correct number of parameters
    if(argc != 10 && flag != 1)
    {
    printf("\n*-*-*-*-*- ERROR -*-*-*-*-*\n");
    printf("Incorrect number of parameters. Run program again with option
    -h for help.\n");
    printf("*-*-*-*-*- ERROR -*-*-*-*-*\n\n");
    }
    else if(argc == 10 && flag != 1)
    {

    transA = *argv[0];
    transB = *argv[1];
    m = atoi(argv[2]);
    n = atoi(argv[3]);
    k = atoi(argv[4]);
    lda = atoi(argv[5]);
    ldb = atoi(argv[6]);
    ldc = atoi(argv[7]);
    alpha=strtod(argv[8], &endptr);
    beta=strtod(argv[9], &endptr);

    if((m == 0 || n == 0 || k == 0 || (alpha == 0 && beta == 0)) && flag
    != 1)
    { //values are equal to zero
    printf("\n*-*-*-*-*- ERROR -*-*-*-*-*\n");
    printf("Result will be zero with the parameters you provided\nRun
    program again with option -h for help.\n");
    printf("\n*-*-*-*-*- ERROR -*-*-*-*-*\n");
    flag = 1;
    }
    else if(((lda < m || lda < 1) || (ldb < k || ldb < 1) || (ldc < m ||
    ldc < 1)) && flag != 1)
    { //values for lda, ldb, ldc are less than 1 or m, n, k
    printf("\n*-*-*-*-*- ERROR -*-*-*-*-*\n");
    printf("One of LDA, LDB, LDC of incorrect length. Run program again
    with option -h for help.\n");
    printf("*-*-*-*-*- ERROR -*-*-*-*-*\n\n");
    flag = 1;
    }
    else if((transA != 'n' && transA != 'N' && transA != 't' && transA
    != 'T' ) && flag != 1)
    {
    //transA not one of n or t
    printf("\n*-*-*-*-*- ERROR -*-*-*-*-*\n");
    printf("TRANS A not one of: n, N, t, T. Run program again with
    option -h for help.\n");
    printf("*-*-*-*-*- ERROR -*-*-*-*-*\n\n");
    flag = 1;
    }
    else if((transB != 'n' && transB != 'N' && transB != 't' && transB
    != 'T') && flag != 1)
    {
    //transB nott one of n or t
    printf("\n*-*-*-*-*- ERROR -*-*-*-*-*\n");
    printf("TRANS B not one of: n, N, t, T. Run program again with
    option -h for help.\n");
    printf("*-*-*-*-*- ERROR -*-*-*-*-*\n\n");
    flag = 1;
    }
    else if(flag != 1)//all is well, proceed.
    {


    //Check whether matrix A and B are transposed
    //then build the array's accordingly
    if(transA == 'n' || transA == 'N')
    {
    if(transB == 'n' || transB == 'N')
    {
    //both A and B are non-trans
    //printf("Initial A\n");
    arrayDim = lda*k;
    fprintf(stderr, "allocating memory to ArrayA\n");
    fprintf (stderr, "dimension: %d\n", arrayDim);
    arrayA= (double *)malloc(sizeof(double)*arrayDim);
    buildMatrix(arrayA, lda, k);

    //printf("Initial B\n");
    arrayDim = ldb*n;
    fprintf(stderr, "allocating memory to ArrayB\n");
    fprintf (stderr, "dimension: %d\n", arrayDim);
    arrayB= (double *)malloc(sizeof(double)*arrayDim);
    buildMatrix(arrayB, ldb, n);

    //printf("Initial C\n");
    arrayDim = ldc*n;
    fprintf(stderr, "allocating memory to ArrayC\n");
    fprintf (stderr, "dimension: %d\n", arrayDim);
    arrayC= (double *)malloc(sizeof(double)*arrayDim);
    buildMatrix(arrayC, ldc, n);

    /*printf("after build A contains = %f\n", arrayA[1]);
    printf("after build B contains = %f\n", arrayB[1]);
    printf("after build C contains = %f\n", arrayC[1]);*/
    }
    else if(transB == 't' || transB == 'T')
    {
    //A is non-trans, B is trans
    //printf("Initial A\n");
    arrayDim = lda*k;
    arrayA= (double *)malloc(sizeof(double)*arrayDim);
    buildMatrix(arrayA, lda, k);

    //printf("Initial B\n");
    arrayDim = ldb*k;
    arrayB= (double *)malloc(sizeof(double)*arrayDim);
    buildMatrix(arrayB, ldb, k);

    //printf("Initial C\n");
    arrayDim = ldc*n;
    arrayC= (double *)malloc(sizeof(double)*arrayDim);
    buildMatrix(arrayC, ldc, n);
    }
    }
    else if(transA == 't' || transA == 'T')
    {
    if(transB == 'n' || transB == 'N')
    {
    //A is trans, B is non-trans
    //printf("Initial A\n");
    arrayDim = lda*m;
    arrayA= (double *)malloc(sizeof(double)*arrayDim);
    buildMatrix(arrayA, lda, m);
    //printf("A0 now contains arrayA[%f]", arrayA[0]);

    //printf("Initial B\n");
    arrayDim = ldb*n;
    arrayB= (double *)malloc(sizeof(double)*arrayDim);
    buildMatrix(arrayB, ldb, n);

    //printf("Initial C\n");
    arrayDim = ldc*n;
    arrayC= (double *)malloc(sizeof(double)*arrayDim);
    buildMatrix(arrayC, ldc, n);
    }
    else if(transB == 't' || transB == 'T')
    {
    //both A and B are trans
    //printf("Initial A\n");
    arrayDim = lda*m;
    arrayA= (double *)malloc(sizeof(double)*arrayDim);
    buildMatrix(arrayA, lda, m);

    //printf("Initial B\n");
    arrayDim = ldb*k;
    arrayB= (double *)malloc(sizeof(double)*arrayDim);
    buildMatrix(arrayB, ldb, k);

    //printf("Initial C\n");
    arrayDim = ldc*n;
    arrayC= (double *)malloc(sizeof(double)*arrayDim);
    buildMatrix(arrayC, ldc, n);
    }
    }


    tempArrayDim = ldc*n;
    fprintf(stderr, "allocating memory to TempArrayC\n");
    fprintf (stderr, "dimension: %d\n", tempArrayDim);
    tempArrayC= (double *)malloc(sizeof(double)*tempArrayDim);

    //buildMatrix(tempArrayC, ldc, n);
    for(f=0; f<tempArrayDim; f++)
    {
    tempArrayC[f] = arrayC[f];
    }

    //generate pointers to the values
    ptrTransA = &transA;
    ptrTransB = &transB;

    ptrM = &m;
    ptrN = &n;
    ptrK = &k;

    ptrLda = &lda;
    ptrLdb = &ldb;
    ptrLdc = &ldc;

    ptrAlpha = &alpha;
    ptrBeta = &beta;

    start.tv_usec=0; stop.tv_usec=0;
    gettimeofday(&start,NULL);

    myMxM(ptrTransA, ptrTransB, ptrM, ptrN, ptrK, ptrAlpha, arrayA,
    ptrLda, arrayB, ptrLdb, ptrBeta, arrayC, ptrLdc);


    gettimeofday(&stop,NULL);
    printf("start =%i..stop =%i..result
    =%i..(microsec)\n",start.tv_usec,
    stop.tv_usec,stop.tv_usec-start.tv_usec);

    e=ldc*n;
    printf("myMxM resulting C matrix\n");
    for(d=0; d<e; d++)
    {
    printf("contents = %.8f\n", arrayC[d]);
    }

    for(f=0; f<tempArrayDim; f++)
    {
    arrayC[f] = tempArrayC[f];
    }
    start.tv_usec=0; stop.tv_usec=0;
    gettimeofday(&start,NULL);

    dgemm_(ptrTransA, ptrTransB, ptrM, ptrN, ptrK, ptrAlpha, arrayA,
    ptrLda, arrayB, ptrLdb, ptrBeta, arrayC, ptrLdc);

    gettimeofday(&stop,NULL);
    printf("start =%i..stop =%i..result
    =%i..(microsec)\n",start.tv_usec,
    stop.tv_usec,stop.tv_usec-start.tv_usec);

    e=ldc*n;
    printf("dgemm resulting C matrix\n");
    for(d=0; d<e; d++)
    {
    printf("contents = %.8f\n", arrayC[d]);
    }

    }

    }//close if(argc>1)


    return 0;
    }
    Dawn Minnis, Mar 15, 2005
    #1
    1. Advertising

  2. Dawn Minnis

    Michael Mair Guest

    Dawn Minnis wrote:
    > Hey guys - this code when called with parameters:
    >
    > driver.o n n 12 12 12 12 12 12 2.6 3.2
    >
    > is kicking back a segmentation fault.
    >
    > I've read the rest of the postings but am still confused. Can someone
    > take a look and tell me how to fix it - please dont be like the guy I
    > spoke to today and tell me that I am not allocating the memory
    > correctly and then walk off. Please, if possible provide me with
    > solutions or at least suggestions to try out.
    >
    > Thanks
    >
    > Dawn
    >

    [snip: lengthy code segment]

    Sorry, your code does not compile for me, as I do not have
    the prototyped functions and <sys/time.h> is no standard header.

    Please provide a compiling minimal example. If it is still
    too long to post here, consider offering the source for download
    and posting a link to it.
    This helps the people round here helping you.


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Mar 15, 2005
    #2
    1. Advertising

  3. Dawn Minnis

    Guest

    Michael Mair wrote:
    > Dawn Minnis wrote:
    > > Hey guys - this code when called with parameters:
    > >
    > > driver.o n n 12 12 12 12 12 12 2.6 3.2
    > >
    > > is kicking back a segmentation fault.
    > >
    > > I've read the rest of the postings but am still confused. Can

    someone
    > > take a look and tell me how to fix it - please dont be like the guy

    I
    > > spoke to today and tell me that I am not allocating the memory
    > > correctly and then walk off. Please, if possible provide me with
    > > solutions or at least suggestions to try out.
    > >
    > > Thanks
    > >
    > > Dawn
    > >

    > [snip: lengthy code segment]
    >
    > Sorry, your code does not compile for me, as I do not have
    > the prototyped functions and <sys/time.h> is no standard header.
    >
    > Please provide a compiling minimal example. If it is still
    > too long to post here, consider offering the source for download
    > and posting a link to it.
    > This helps the people round here helping you.
    >
    >
    > Cheers
    > Michael
    > --
    > E-Mail: Mine is an /at/ gmx /dot/ de address.


    Hey Michael

    Sorry for the confusion - but I'm a bonefide dimwit. In one of the
    functions I was using malloc and multiplying by the dimension "0". Yes
    zero, not a genuine dimension that was nicely passed in, no no, zero.

    I am living proof you should never try and code or debug when tired.

    Thanks again for your time and patience - and such a quick response.

    Dawn
    , Mar 15, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bin Lu

    malloc segmentation fault

    Bin Lu, Oct 3, 2003, in forum: C Programming
    Replies:
    9
    Views:
    7,446
    Mark McIntyre
    Oct 23, 2003
  2. Alexandre

    Segmentation fault in mallopt/malloc call

    Alexandre, Feb 18, 2005, in forum: C Programming
    Replies:
    7
    Views:
    461
    RoSsIaCrIiLoIA
    Mar 6, 2005
  3. Zheng Da
    Replies:
    3
    Views:
    11,538
  4. I_have_nothing
    Replies:
    6
    Views:
    797
    I_have_nothing
    Jun 15, 2005
  5. segmentation fault, malloc error

    , Nov 12, 2007, in forum: C Programming
    Replies:
    7
    Views:
    778
Loading...

Share This Page