Marco Devillers
5/25/2011 3:21:00 PM
On May 25, 4:53 pm, Tom St Denis <t...@iahu.ca> wrote:
> On May 25, 8:53 am, Marco Devillers <marco.devill...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
> > Hi all, a short question.
>
> > I wrote a compiler which compiles to C and has a garbage collector
> > written in C. The garbage collector assumes that nodes in memory are
> > arrays of integers or pointers (I use the default integer of pointer
> > size type for that).
>
> > However, sometimes, terminal nodes may hold other values, such as
> > floats, doubles, etc. Naturally, I cast these then to the correct
> > type, so a intptr_t* becomes a float*. Of course, this is unspecified
> > behavior according to the C standard, so I need to compile with the -
> > fno-strict-aliasing switch in gcc.
>
> > I want to rewrite my code such that I don't need that switch anymore,
> > i.e., I want to end up with portable code.
>
> > My question: What is the correct manner to rewrite this code?
>
> > I know there's an exception to strict aliasing, i.e., the char
> > pointer. Should I redefine the node pointer type as char*, or is it
> > sufficient to cast intptr_t* to char* and then to float* (can I use a
> > char* as an intermediate to trick the C type system)?
>
> As Jacob pointed out use a "void *" pointer in your terminal node so
> you can assign any pointer type to it. You might want to have some
> form of metadata along for the ride so you know what type the node is
> when using it...
>
> Tom
The metadata is -of course- already present; size and type tags
discriminate all terminal nodes.
Terminal nodes hold series of bits, integers or pointers, but also may
hold data, not pointers to data. I don't see how your solution would
work.