anything wrong with this?

Discussion in 'C Programming' started by atv, Dec 13, 2003.

  1. atv

    atv Guest

    Hi,
    is there something wrong with this code? It seems that it crashes
    at the else { clause, when allocating a second record. Excuse for
    the indentation, the g_warnings are from gtk.

    Also, i'm using 2 pointers in the struct, which consists of 3 ints,
    and 2 pointers, which i give 256 bytes each. Much to much ofcourse,
    but it's just testcode.

    The code doesn't even reach the g_warning in the { code.
    Any help most appreciated.
    atv

    if(tail==NULL) {
    g_warning("First record in schedule allocated");
    tail=(schedule *)malloc(256);
    tail->string=malloc(256);
    tail->timestamp=malloc(256);
    strcpy(tail->string,string);
    strcpy(tail->timestamp,asctime(tstruct));
    tail->year=tstruct->tm_year;
    tail->month=tstruct->tm_mon;
    tail->day=tstruct->tm_mday;
    tail->next=NULL;
    head=tail;
    } else {
    g_warning("New record in schedule allocated");
    /* First check if a day is already booked */
    while(tmp!=NULL) {
    g_warning("Checking for double appointment");
    if(!strcmp(tmp->timestamp,asctime(tstruct))) {
    gtk_widget_hide(window1);
    return -1;}
    tmp=tmp->next;
    }
    tail->next=(schedule *)malloc(256);
    tail=tail->next;
    tail->string=malloc(256);
    tail->timestamp=malloc(256);
    strcpy(tail->string,string);
    strcpy(tail->timestamp,asctime(tstruct));
    tail->year=tstruct->tm_year;
    tail->month=tstruct->tm_mon;
    tail->day=tstruct->tm_mday;
    tail->next=NULL;
    atv, Dec 13, 2003
    #1
    1. Advertising

  2. atv <> wrote in news:3fdb7aee$0$207
    $4all.nl:

    > Hi,
    > is there something wrong with this code? It seems that it crashes
    > at the else { clause, when allocating a second record. Excuse for
    > the indentation, the g_warnings are from gtk.


    Well, you can't expect much if we can't compile your code.

    > if(tail==NULL) {
    > g_warning("First record in schedule allocated");
    > tail=(schedule *)malloc(256);


    No need to cast malloc's return value. What is tail? Why are you allocating
    256 chars when you need to allocate sizeof(*tail) chars? How do you know
    the allocation succeeded?

    > tail->string=malloc(256);
    > tail->timestamp=malloc(256);


    How do you know the allocation succeeded?

    > strcpy(tail->string,string);


    Do you know if string points to something that is less than 256 chars long?

    Please try to provide a short example which we can compile and still
    exhibits the problem.
    --
    A. Sinan Unur

    Remove dashes for address
    Spam bait: mailto:
    A. Sinan Unur, Dec 13, 2003
    #2
    1. Advertising

  3. atv wrote:

    > Hi,
    > is there something wrong with this code?


    Yes.

    > It seems that it crashes
    > at the else { clause, when allocating a second record.


    If there were nothing wrong with the code, it wouldn't crash, would it?

    > Excuse for
    > the indentation, the g_warnings are from gtk.
    >
    > Also, i'm using 2 pointers in the struct, which consists of 3 ints,
    > and 2 pointers, which i give 256 bytes each. Much to much ofcourse,
    > but it's just testcode.
    >
    > The code doesn't even reach the g_warning in the { code.
    > Any help most appreciated.
    > atv
    >


    Well, the first thing to do is see what the compiler has to say...

    > if(tail==NULL) {
    > g_warning("First record in schedule allocated");
    > tail=(schedule *)malloc(256);
    > tail->string=malloc(256);
    > tail->timestamp=malloc(256);
    > strcpy(tail->string,string);
    > strcpy(tail->timestamp,asctime(tstruct));
    > tail->year=tstruct->tm_year;
    > tail->month=tstruct->tm_mon;
    > tail->day=tstruct->tm_mday;
    > tail->next=NULL;
    > head=tail;
    > } else {
    > g_warning("New record in schedule allocated");
    > /* First check if a day is already booked */
    > while(tmp!=NULL) {
    > g_warning("Checking for double appointment");
    > if(!strcmp(tmp->timestamp,asctime(tstruct))) {
    > gtk_widget_hide(window1);
    > return -1;}
    > tmp=tmp->next;
    > }
    > tail->next=(schedule *)malloc(256);
    > tail=tail->next;
    > tail->string=malloc(256);
    > tail->timestamp=malloc(256);
    > strcpy(tail->string,string);
    > strcpy(tail->timestamp,asctime(tstruct));
    > tail->year=tstruct->tm_year;
    > tail->month=tstruct->tm_mon;
    > tail->day=tstruct->tm_mday;
    > tail->next=NULL;


    foo.c:1: parse error before `if'
    foo.c:3: warning: type defaults to `int' in declaration of `tail'
    foo.c:3: `schedule' undeclared here (not in a function)
    foo.c:3: parse error before `)'
    foo.c:6: parse error before `->'
    foo.c:6: warning: type defaults to `int' in declaration of `strcpy'
    foo.c:6: ANSI C forbids data definition with no type or storage class
    foo.c:7: parse error before `->'
    foo.c:12: warning: type defaults to `int' in declaration of `head'
    foo.c:12: initializer element is not constant
    foo.c:12: ANSI C forbids data definition with no type or storage class
    foo.c:13: parse error before `}'
    foo.c:21: warning: type defaults to `int' in declaration of `tmp'
    foo.c:21: invalid type argument of `->'
    foo.c:21: ANSI C forbids data definition with no type or storage class
    foo.c:22: parse error before `}'
    foo.c:24: warning: type defaults to `int' in declaration of `tail'
    foo.c:24: redefinition of `tail'
    foo.c:3: `tail' previously defined here
    foo.c:24: invalid type argument of `->'
    foo.c:24: ANSI C forbids data definition with no type or storage class
    foo.c:25: parse error before `->'
    foo.c:27: parse error before `->'
    foo.c:27: warning: type defaults to `int' in declaration of `strcpy'
    foo.c:27: ANSI C forbids data definition with no type or storage class
    foo.c:28: parse error before `->'
    make: *** [foo] Error 1

    Perhaps getting it to compile relatively cleanly would be a sound first step
    towards a solution.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Dec 14, 2003
    #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. Mano kumar
    Replies:
    2
    Views:
    355
    Kevin Spencer
    Oct 17, 2003
  2. Replies:
    5
    Views:
    504
  3. rf
    Replies:
    1
    Views:
    366
    Leif K-Brooks
    Jul 6, 2003
  4. John Black
    Replies:
    3
    Views:
    517
    Dave Moore
    Jun 2, 2004
  5. ES Kim
    Replies:
    14
    Views:
    596
    Rob Williscroft
    Sep 27, 2004
Loading...

Share This Page