help-bison
[Top][All Lists]
Advanced

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

Re: bison and malloc


From: Tim Van Holder
Subject: Re: bison and malloc
Date: Wed, 11 Oct 2006 17:17:54 +0200
User-agent: Thunderbird 1.5.0.7 (Windows/20060909)

s88 wrote:
> On 10/11/06, Tim Van Holder <address@hidden> wrote:
>>
>> s88 wrote:
>> > Hi all:
>> >   I'm confused with the bison and malloc right now...
>> > I'm using the Bison 2.0 on Ubuntu(Linux) 6.06, and everytime when I use
>> > the malloc(actually is strdup) in the production rules. It will
>> cause an
>> > unpredictable
>> > segmentation fault. The stack size on my system is set unlimited.
>> > Every time after using the dynamic allocate memory, I'll free the
>> memory
>> by
>> > myself.
>> >
>> > What cause this fault, and how to fix it?
>> >
>> > Any idea?
>> >
>> > Thankx!!!!!
>> > Dave.
>>
>> The most likely cause would be that you're free()ing twice.
>> But without a little bit of code to illustrate what you're doing, it's
>> very hard to diagnose a crash bug.
>>
>>
>>
> well..thank U all first!!
> Let me show my code here...
> I want to parse the sequence such likes X1,X2,{Y1,X4,Z5}
> BTW, I'm using the GTK library to malloc and free, but I'm sure the
> question
> is not on the GTK.
> 
> 
> arguements : arguement{

Minor nit - the correct English word is argument, not arguement.

>        $$ = $1;
> }| arguements ',' arguement{
>        GList *_l = g_list_concat($1,$3);
>        $$ = _l;

names with leading underscores are reserved - don't use them.
Here, "$$ = g_list_concat($1,$3);" should be enough.

> };
> 
> 
> arguement:'{' arguements '}' {
>        $$ = $2;
> }|'[' arguements ']' {
>        $$ = $2;
> }|UID{
>        $$ = create_arglist($1);
> }|ID{
>        $$ = create_arglist($1);
> }|INTEGER{
> }|EUINT64VAL{

It's not clear what kind of value is represented by INTEGER and
EUINT64VAL - I'll assume it's a GList* with a single element in the
list.

> };
> 
> #include <gtk/gtk.h>
> GList* create_arglist(const gchar* str){
>        GList* _list = NULL;
>        gchar* _str  = g_strdup(str);
>        _list = g_list_append(_list,_str);
>        return _list;
> }

Simply "return g_list_append(NULL, g_strdup(str));" would be equivalent.

> 
> void arglist_free(GList* list){
>        GList *_cur = list;
>        GList *_next;
>        while(_cur !=NULL){
>                _next = _cur->next;
>                g_free(_cur->data);
>                _cur = _next;
>        }
>        g_list_free(list);
> }


A separate next field isn't needed, as you're not freeing _cur
separately, so

void
arglist_free(GList* list)
{
GList* walker = list;
  while (walker != NULL) {
    g_free (walker->data);
    walker = walker->next;
  }
  g_list_free(list);
}

In any case, the code you show is generally fine, nothing here that
would segfault at first glance.
I'd recommend at least compiling your program with debug info and
running it under gdb so you can at least see where the segfault occurs
exactly (and as suggested, consider running it under valgrind (if you're
on linux), as it may detect the problem with greater accuracy).
If you're still stuck after that, consider posting in some C or GTK
newsgroup, as it does not seem that what is happening is in any way
caused by bison (note: if you have 'error' rules in your grammar, be
careful with freeing token values, as you may end up freeing them
twice).





reply via email to

[Prev in Thread] Current Thread [Next in Thread]