Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2000-08-24 08:15:15


----- Original Message -----
From: "Bill Wade" <bill.wade_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Thursday, August 24, 2000 8:41 AM
Subject: RE: [boost] low-level sub-allocation/alignment challenge

> > -----Original Message-----
> > From: Branko Èibej [mailto:branko.cibej_at_[hidden]]
>
> > Greg Colvin wrote:
> > >
> > > I think if you do it like this
> > >
> > > struct Rule
> > > {
> > > struct Fixed {
> > > unsigned long cost;
> > > const void* action;
> > > } fixed;
> > > const char* symbols[1]; // actually a variable-length array
> > > };
> > >
> > > then you can align rules on sizeof(Rule::Fixed) boundaries.
> >
> > The alignment requirements for Rule might be stricter than the
> > requirements for Rule::Fixed.
>
> Compilers are allowed to be silly in their alignment rules, however I'd be
> surprised to see a real compiler where the alignment for a struct wasn't
the
> greater of
>
> 1) Some minimum alignment requirement for all structs.
> 2) The largest alignment requirement for any one of the struct's
members.
>
> If a compiler follows these rules the alignment requirement for Fixed is
the
> same as the alignment requirement for Rule. In fact you can do a little
> better:
>
> struct foo { union { long a; void* b; }; };
>
> If a compiler follows (1) and (2), the alignment requirement for Rule is
no
> greater than sizeof(foo);

Okay, but consider the case where struct Fixed has nothing but a single char
member:
struct Rule {
   struct Fixed {
      char x;
   } fixed;
   const char* symbols[1];
};

It's pretty likely, don't you think, that Fixed and Rule have different
alignments?

How about this, for determining the alignment requirements of any POD:

template <class PodT>
class alignment_of
{
    struct align {
        char misalign;
        PodT x;
    };
public:
    const std::size_t value = offsetof(align, x);
};

-Dave


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk