Related articles |
---|
bison question (memory deallocation) adrian@cam.cornell.edu (1999-08-12) |
From: | adrian@cam.cornell.edu (Adrian Mariano) |
Newsgroups: | comp.compilers |
Date: | 12 Aug 1999 02:59:29 -0400 |
Organization: | Cornell University |
Keywords: | yacc, storage, question, comment |
I'm implementing a parser in bison which has a pointer to the
following structure specified as one option in the "%union" statement
for bison:
struct unittype {
char *numerator[MAXSUBUNITS];
char *denominator[MAXSUBUNITS];
double factor;
};
In the process of scanning and reducing, various instances of this
structure get allocated and entries of the numerator[] and
denominator[] arrays also get allocated. A well behaved program
should free this stuff at some point. When and how do I free it?
Is it ok for my actions to destroy their arguments? So like an action
that does { $$ = foo($1, $2); kill($1);kill($2);} after which time $1
and $2 (which are pointers to struct unittype) no longer point
anywhere valid?
What about when parse errors occur? Is there some way I can clean up
the parser stack and free anything on it that needs to be freed? Or
do I have to do my own memory management?
[In principle you could put in error rules and catch everything as yacc
unwinds the stack, in practice that's too fragile to do. I use a malloc
wrapper that chains together everything it allocates, then after the
parse, I zip down the chain and free everything. -John]
Return to the
comp.compilers page.
Search the
comp.compilers archives again.