[Dynamite] ANSI C question

Guy Slater guy@ebi.ac.uk
Thu, 23 Mar 2000 15:21:13 +0000 (GMT)


On Thu, 23 Mar 2000, Ewan Birney wrote:

> On Wed, 22 Mar 2000, Ian Holmes wrote:
> 
> > GLib uses casts to implement "private" data, e.g.
> > 
> > struct _GArray
> > {
> >   gchar *data;
> >   guint len;
> > };
> > 
> > in "glib.h", which is then casted to
> > 
> > struct _GRealArray
> > {
> >   guint8 *data;
> >   guint   len;
> >   guint   alloc;
> >   guint   elt_size;
> >   guint   zero_terminated : 1;
> >   guint   clear : 1;
> > };
> > 
> > Do either of you know if this is strict ANSI, i.e. can we be sure that the
> > first two fields of _GArray (data & len) are mapped to the same memory
> > locations as data and len in _GRealArray in all C compilers?
> 
> This is strict ANSI interestingly. (read the back of K&R about struct
> layout).

Given that:

    o The first member must always have offset zero
    o Other members have increasing offset
    o Structure padding is implementation defined

It still isn't clear to me (from K&R) whether the padding
has to be *consistent* throughout the implemenataion.

Anyway, I doubt the glib people wouldn't have used it if
it wasn't safe - they seem to have gone to a lot of effort
to ensure portability. 

> > If so it makes life a lot easier...

Saves much messing with offsetof() or somesuch ..

> This trick is what GTK uses for inheritance.
>
> > 
> > Ian
> > 
> > -- 
> > Ian Holmes  ....  Howard Hughes Medical Institute  ....  ihh@fruitfly.org
> > 
> > 
> > _______________________________________________
> > Dynamite mailing list  -  Dynamite@bioperl.org
> > http://www.bioperl.org/mailman/listinfo/dynamite
> > 
> 
> -----------------------------------------------------------------
> Ewan Birney. Mobile: +44 (0)7970 151230
> <birney@ebi.ac.uk>
> -----------------------------------------------------------------
> 
> 
> _______________________________________________
> Dynamite mailing list  -  Dynamite@bioperl.org
> http://www.bioperl.org/mailman/listinfo/dynamite
>