[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Not able to use %union?
From: |
Christian Schoenebeck |
Subject: |
Re: Not able to use %union? |
Date: |
Mon, 18 Feb 2019 12:57:12 +0100 |
User-agent: |
KMail |
On Montag, 18. Februar 2019 12:22:27 CET Christian Schoenebeck wrote:
> On Sonntag, 17. Februar 2019 21:37:26 CET Peng Yu wrote:
> > Because I want to use the previously allocated memory, I don't want to
> > call "rs_init(&yylval->str)" in any action. So YYSTYPE must be a
> > struct instead of a union. Is it a good practice to use struct instead
> > of union?
>
> Yes, it is. The only advantage by using a union as a type is that you save
> some tiny amount of memory, but a union is also less safe and harder to
> debug. So personally I would barely ever use union as fundamental type with
> Bison.
Btw, you can also mix both approaches, e.g.:
struct _YYSTYPE {
union {
int inum;
float fnum;
};
rapidstring str;
Foo_t foo;
};
typedef it appropriately, I usually use C++ code, but you get the idea.
Best regards,
Christian Schoenebeck