help-bison
[Top][All Lists]
Advanced

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

Re: How to correctly deallocate some token types ...


From: Hans Aberg
Subject: Re: How to correctly deallocate some token types ...
Date: Mon, 12 May 2003 11:21:44 +0200

At 00:27 +0100 2003/05/12, Ricardo Rafael wrote:
>Hi,
>
>       given the following declarations:
>
>
>%union
>{
>    BOOL        boolean_val;
>    int         integer_val;
>    char      * string_val;
>    SomeClass * action;
>
>    struct
>    {
>      SomeClass * items;
>      int         length;
>    } n_list;
>
>    struct expression
>    {
>      SomeClass * a,
>                * b,
>                * c;
>    } expression;
>} // YYSTYPE
...
>    I have the following questions:
>
>       a) Should I care to deallocate BOOLEAN_TOKEN and INTEGER_TOKEN?

You should only deallocate what has been created with malloc() etc. or else
you will cause a formal memory error. In the example above, it looks as
though they are use static data allocation, and should thus not be
deallocated.

Try to imagine the data you are creating: The stack itself is only using
static data allocations, but some data may be pointers that point into the
heap. The program does not know that this heap data should be marked
available, unless you somehow tell it. The stuff on the stack itself need
not be worried about.

>%destructor { delete $$; } arg_list
>%destructor { delete $$; } expr

What is this "delete": Isn't that for use with C++? If you use C++ and the
C stacks, you should be aware of that the parser C stack does not invoke
any C++ copy contructors.

>       b) Do I need to deallocate anything else in case of
>non-terminals: <arg_list> and <expr>?

Again, you must picture what you have created on the heap and deallocate
it. If your SomeClass above is a recursive structure which involves
multiple uses of malloc() then you need to recurse through this stuff at
deallocation time using one free() for each malloc().

C++ is doing this recursion automatically. But with C++, one cannot use
%union, as that is implemented using a C/C++ union, which does not admit
data types with non-trivial constructors.


  Hans Aberg






reply via email to

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