mystery of calloc with #define

Discussion in 'C Programming' started by a, Nov 29, 2007.

  1. a

    a Guest

    By James Kuyper insightful suggestion, I identified my program error sources
    from calloc.

    I just don't know why for the following small program, I try different
    JUDGEMENTDAY from 1,2,... and when it is >= 13, segmentation fault appears
    if I don't force the program to exit by setting k to <= 11 in the last but 4
    line. I guess the program is accessing something invalid but just don't know
    why this happens.

    #define SIZE 12
    #define JUDGEMENTDAY 13

    #include <stdlib.h>
    #include <stdio.h>
    int main(int argc, char **argv) {

    int i=0;

    int **chair_assg;
    // int chair_assg[JUDGEMENTDAY+1][SIZE];
    //int chair_assg[JUDGEMENTDAY][SIZE];
    int t=0,k,kk,j,jj,mid,did;
    double cost;

    ///*
    i = JUDGEMENTDAY + 1;
    printf("i: %d",i);
    //return;
    chair_assg = calloc(i,sizeof(int*));
    if(chair_assg == NULL)
    return;
    //chair_assg = calloc((JUDGEMENTDAY),sizeof(int*));
    for(i=0;i<SIZE;i++) {
    chair_assg = calloc(SIZE,sizeof(int));
    }

    for(k=0;k<JUDGEMENTDAY;k++) {
    printf("k: %d", k);
    for(i=0;i<SIZE;i++) {
    chair_assg[k] = 0;
    }
    if (k==11) // <-------------- HERE
    return;
    }
    return 0;
    }
     
    a, Nov 29, 2007
    #1
    1. Advertisements

  2. Here's your program with the commented-out bits removed, and indented
    for readability:

    #define SIZE 12
    #define JUDGEMENTDAY 13

    #include <stdlib.h>
    #include <stdio.h>
    int main(int argc, char **argv) {
    int i=0;
    int **chair_assg;
    int t=0,k,kk,j,jj,mid,did;
    double cost;

    i = JUDGEMENTDAY + 1;
    printf("i: %d",i);

    chair_assg = calloc(i,sizeof(int*));
    if(chair_assg == NULL)
    return;

    for(i=0;i<SIZE;i++) {
    chair_assg = calloc(SIZE,sizeof(int));
    }

    for(k=0;k<JUDGEMENTDAY;k++) {
    printf("k: %d", k);
    for(i=0;i<SIZE;i++) {
    chair_assg[k] = 0;
    }
    }

    return 0;
    }

    You seem to be confused about which dimension is SIZE and which is
    JUDGEMENTDAY.

    You use JUDGEMENTDAY+1 (=14) for the first dimension (rows) when
    calloc()ing it (what was the +1 for?). Then you count up to SIZE (12)
    when assigning calloc()ed space for each row. So you have only
    allocated space for 12 of the 14 rows.

    The you assign 0 to SIZE (12) columns in each of JUDGEMENTDAY (13)
    rows - but only 12 of those rows have been allocated.

    -- Richard
     
    Richard Tobin, Nov 29, 2007
    #2
    1. Advertisements

  3. chair_assg has JUDGEMENTDAY+1 elements..


    Here's the error: you assign the elements 0-(SIZE-1), but "SIZE" has
    nothing to do with the number of elements in chair_assign. Actually the
    for statements should be

    for(i=0;i<JUDGEMENTDAY+1;i++) // The +1 is from your code.
    {
    chair_assg = calloc(SIZE,sizeof(int));
    if (chair_assg==0) return 1;
    }

    You mixed up the array dimensions.

    This breaks for k==12 here, because chair_assg[12] has never been
    assigned.
     
    Marco Manfredini, Nov 29, 2007
    #3


  4. Read this piece of code, there is the error.
     
    Dik T. Winter, Nov 29, 2007
    #4
  5. Didn't your compiler generate a diagnostic because you are returning
    from main but not returning an int as promised by the declaration?


    Funny that you don't feel the need to check the return from calloc as
    you did above.
    This should print k: 0k: 1k: 2. Unless your terminal uses paper,
    adding a \n to your format string does not cost any more money.


    Why are you doing this since the space was already zeroed out by
    calloc?
    Others have explained how your inconsistent indices have resulted in
    attempting to dereference a pointer whose value is all bits zero
    (possibly a NULL pointer).

    Remove del for email
     
    Barry Schwarz, Dec 2, 2007
    #5
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.