|Reclaiming dynamic memory if parse fails? firstname.lastname@example.org (email@example.com) (2007-07-13)|
|Re: Reclaiming dynamic memory if parse fails? firstname.lastname@example.org (Chris Dollin) (2007-07-16)|
|Re: Reclaiming dynamic memory if parse fails? DrDiettrich1@aol.com (Hans-Peter Diettrich) (2007-07-16)|
|Re: Reclaiming dynamic memory if parse fails? email@example.com (Mark Holland) (2007-07-16)|
|Re: Reclaiming dynamic memory if parse fails? firstname.lastname@example.org (Martin Ward) (2007-07-16)|
|Re: Reclaiming dynamic memory if parse fails? email@example.com (Matthias Blume) (2007-07-18)|
|Date:||Fri, 13 Jul 2007 14:50:34 -0000|
|Keywords:||parse, errors, question, comment|
|Posted-Date:||15 Jul 2007 16:28:15 EDT|
I'm using lex and yacc for parsing a configuration file that builds a
dynamically allocated tree structure for runtime searching as user
requests come in. When a parse fails, in the middle of the
configuration file, and the tree is partially built, how can I reclaim
all of the dynamic memory gracefully? The tree is built from the
bottom up, the references to the pointers of the tree are passed
between each of the grammar rules using the $$ = (some pointer)
scheme, finally the root of the tree is set at the start symbol.
I thought of having a global array of some structure holding a pointer
to each dynamically allocated structure and an enum to identify the
type of the object. Then as each item is allocated, it is added to
the array. Then if yyerror is called, it could free all of the
references in that list. This method seems clumsy to me though...
Any other ideas?
[There's no really clean way to do it. Allocating out of a fixed
arena is one popular approach. I've used a wrapped version of malloc
that chains all the allocated chunks together, then run down the chain
and free them all when it's done. -John]
Return to the
Search the comp.compilers archives again.