On Tue, 8 Jul 2008, Andreas Schwab wrote:

> "Kaveh Ghazi" <[EMAIL PROTECTED]> writes:
>
> > Right, here's the original link where I mention it:
> > http://gcc.gnu.org/ml/gcc-patches/2008-06/msg01658.html
> >
> > This involves a cast from one type to another through a void*.  I haven't
> > been able to convince myself that this is completely safe.  If someone
> > understands why the code is violating type-safety intentionally and can
> > explain why it's always safe, I would be happy to insert the appropriate
> > cast and fix it.
>
> The two types bitmap_element and bitmap_head share a common initial
> sequence, and when following through ->heads only the first pointer is
> ever used.  If you define a union type containing the two structs and
> make it visible at any point using that cast that would make it
> completely safe to the letter of the standard (6.5.2.3#5).
> Andreas.
Thanks for the explanation.  However I don't understand why these
conditions make it "safe".  First, while these two structs share an
initial sequence of members, they don't seem to mean the same thing, i.e.
they are of type bitmap_element and named next/prev vs first/current.
Second, we can't guarantee that only the first pointer is ever used
because in both cases the bitmap object escapes back out of the function
and we have no control over how it's handled.

It seems to me that the union trick is just a way to violate type-safety
without seeing any warnings, same as casting to void* was. :-)

Needless to say, I don't understand this (IMHO convoluted) part of the
bitmap implementation.  So I'd rather leave it to someone who does.

                Thanks,
                --Kaveh
--
Kaveh R. Ghazi                  [EMAIL PROTECTED]

Reply via email to