[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: C programming: Segmentation fault within malloc?



> 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 <shaulka@bezeqint.net> 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 <shaulka@bezeqint.net>
> > 
> > 
> > 
> > -- 
> > To UNSUBSCRIBE, email to debian-user-request@lists.debian.org 
> > with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
> > 
> 
> -- 
> Shao Zhang                          Tel:  (02) 9209 4838
> Software Engineer                   Fax:  (02) 9209 4992
> Redfern Broadband Networks (RBN)    Mail: szhang@rbni.com
> 
> 
> -- 
> To UNSUBSCRIBE, email to debian-user-request@lists.debian.org 
> with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
> 

-- 

	Shaul Karl <shaulka@bezeqint.net>




Reply to: