Reclaiming dynamic memory if parse fails?

"carsonbj@gmail.com" <carsonbj@gmail.com>
Fri, 13 Jul 2007 14:50:34 -0000

          From comp.compilers

Related articles
Reclaiming dynamic memory if parse fails? carsonbj@gmail.com (carsonbj@gmail.com) (2007-07-13)
Re: Reclaiming dynamic memory if parse fails? chris.dollin@hp.com (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? kenshin_40@htomail.com (Mark Holland) (2007-07-16)
Re: Reclaiming dynamic memory if parse fails? martin@gkc.org.uk (Martin Ward) (2007-07-16)
Re: Reclaiming dynamic memory if parse fails? blume@hanabi.local (Matthias Blume) (2007-07-18)
| List of all articles for this month |
From: "carsonbj@gmail.com" <carsonbj@gmail.com>
Newsgroups: comp.compilers
Date: Fri, 13 Jul 2007 14:50:34 -0000
Organization: Compilers Central
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?


Thanks.
[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]


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.