> > Try breaking the mallocs into separate lines to see which one fails: > > int errflag = 0; > if (!(node = (struct node *)malloc(sizeof(struct node)))) { > errflag = 1; > } else { > if (!(node->data = (struct symbol *)malloc(sizeof(struct > symbol)))) { > errflag = 2; > } > } > if (errflag) { > fprintf(stderr, "errflag=%d\n"); > fprintf(stderr, sym_tab_msg[MEMORY_ALLOCATION_FAILURE]); > return FALSE; > } > > If the first one sets errflag without a segfault in malloc, it would seem > that your original compound statement tried to do the second malloc. The > second malloc would certainly fail if the first malloc results in "node" > being a null pointer, because it would try to set node->data. You can try > to test this with:
The C language guarantees that with such AND logical expression, the 2nd operand will not be executed unless the first operand has a logical value of TRUE. The real problem was that I was convinced that realloc(void*, size_t) does not free the previously allocated area; and therefore have free it myself. That was done much earlier then when malloc segfault since the error manifested itself only when malloc had to run again. > > if (!((node = (struct node *)malloc(sizeof(struct node))) && > (fprintf (stderr, "should not get here\n")))) { > } > > > On Thu, 26 Jul 2001, Shaul Karl wrote: > > > > Have you tried to look at the value sizeof(struct node)? It might be too > > > big. > > > Otherwise, can you show us the backtrace in gdb. > > > > > > > > > gdb says sizeof(struct node) == 20. It is mostly a couple of pointers: > > > > struct node { > > enum colors color; > > struct node *left, *right, *parent; > > void *data; > > }; > > > > > > Breakpoint 1, insert_symbol (sym=0xbfffe25c) at symbols.c:197 > > 197 if (!((node = (struct node *)malloc(sizeof(struct node))) && > > (gdb) p sizeof(struct node) > > $1 = 20 > > (gdb) c > > Continuing. > > > > Program received signal SIGSEGV, Segmentation fault. > > 0x400af19e in malloc () from /lib/libc.so.6 > > (gdb) bt > > #0 0x400af19e in malloc () from /lib/libc.so.6 > > #1 0x400ae844 in malloc () from /lib/libc.so.6 > > #2 0x804c60a in insert_symbol (sym=0xbfffe25c) at symbols.c:197 > > #3 0x8048c6f in tmp_variable (tmp=0xbfffe398, sym_data=0xbfffe2ec) > > at actions.c:115 > > #4 0x80497be in sym_for_const (tok=0xbfffe4f8, sym=0xbfffe398) > > at actions.c:412 > > #5 0x804d55f in yyparse () at parser.y:216 > > #6 0x804ddd5 in translator (params=0xbffffb80) at parser.y:272 > > #7 0x804b08f in main (argc=1, argv=0xbffffc98) at main.c:195 > > #8 0x4005b2db in __libc_start_main () from /lib/libc.so.6 > > (gdb) > > > > I believe that malloc is called twice here due to line 198: > > > > if (!((node = (struct node *)malloc(sizeof(struct node))) && > > (node->data = malloc(sizeof(struct symbol))))) { > > > > and sizeof(struct symbol) is 52. > > > > > > > > > On [Thu, 26 Jul 2001 03:15:46 +0300], Shaul Karl <[EMAIL PROTECTED]> > > > wrote: > > > > Breakpoint 2, insert_symbol (sym=0xbfffe25c) at symbols.c:197 > > > > 197 if (!((node = (struct node *)malloc(sizeof(struct node))) > > > > && > > > > (gdb) l 197 > > > > 192 > > > > 193 enum flag insert_symbol(struct symbol *sym) > > > > 194 { > > > > 195 struct node *node; > > > > 196 > > > > 197 if (!((node = (struct node *)malloc(sizeof(struct node))) > > > > && > > > > 198 (node->data = (struct symbol *)malloc(sizeof(struct > > > > symbol))))) { > > > > 199 fprintf(stderr, sym_tab_msg[MEMORY_ALLOCATION_FAILURE]); > > > > 200 return FALSE; > > > > 201 } > > > > (gdb) n > > > > > > > > Program received signal SIGSEGV, Segmentation fault. > > > > 0x400af19e in malloc () from /lib/libc.so.6 > > > > (gdb) > > > > > > > > > > > > How can it be? If malloc can not allocate memory it should return a > > > > NULL > > > > pointer. How can it Seg fault? > > > > > > > > [03:09:45 16]$ free > > > > total used free shared buffers > > > > cached > > > > Mem: 63584 60936 2648 31452 1344 > > > > 20472 > > > > -/+ buffers/cache: 39120 24464 > > > > Swap: 116924 52580 64344 > > > > [03:09:49 16]$ > > > > > > > > Since all the memory is used and the machine is running for some time > > > > now, > > > > doesn't that precludes hardware problems? > > > > > > > > Obviously I am missing something. > > > > > > > > > > > > -- > > > > > > > > Shaul Karl <[EMAIL PROTECTED]> > > > > > > > > > > > > > > > > -- > > > > To UNSUBSCRIBE, email to [EMAIL PROTECTED] > > > > with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED] > > > > > > > > > > -- > > > Shao Zhang Tel: (02) 9209 4838 > > > Software Engineer Fax: (02) 9209 4992 > > > Redfern Broadband Networks (RBN) Mail: [EMAIL PROTECTED] > > > > > > > > > -- > > > To UNSUBSCRIBE, email to [EMAIL PROTECTED] > > > with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED] > > > > > > > -- > > > > Shaul Karl <[EMAIL PROTECTED]> > > > > > > > > -- > > To UNSUBSCRIBE, email to [EMAIL PROTECTED] > > with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED] > > > > > > ...RickM... > -- Shaul Karl <[EMAIL PROTECTED]>