help-bison
[Top][All Lists]
Advanced

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

Re: %union errors that shouldn't be there


From: Laurence Finston
Subject: Re: %union errors that shouldn't be there
Date: Sat, 26 Mar 2005 17:50:10 +0100
User-agent: IMHO/0.98.3+G (Webmail for Roxen)

> With unions, one wants to avoid dynamic allocations. Each dynamic 
> allocation takes several tens, sometimes, hundreds of cycles.

If pointers are used, then memory needs to be allocated for the objects they
point to, whether the pointers are in a `struct' or a `union'.  It needn't be
allocated dynamically in either case.

I don't see any advantage to having multiple 
members of pointer types in a `union', e.g.,

union
{
   A* a;
   B* b;
   C* c;
   ...
};

In practice, it will probably make some code simpler and some more
complicated.
It's certainly not needed in order for the compiler to calculate the size
needed for the `union'.  I would prefer

union
{
   void* v;
   ...
};

but off the top of my head, I don't see any decisive advantages or
disadvantages either way for automatically generated code.  For user code, I
think the `void* v' variant would be easier.

> Unions are faster than dynamic allocations, and in the past, it was 
> important that they take little space. 

I have nothing against `unions'.  I think if pointers are going to be used
anyway, then a single `void*' could be used for all types, in which case there
would be no need for a `union'.   If class types are to be allowed, the
constructors of objects of these types might be performing dynamic allocation,
so it might not pay to take the trouble of avoiding it for the other types.  I
think it's an interesting problem.

Laurence



reply via email to

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