anything wrong with this?

A

atv

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

A. Sinan Unur

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.
 
R

Richard Heathfield

atv said:
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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top