|
Boost : |
From: Martin Bonner (martin.bonner_at_[hidden])
Date: 2006-03-07 10:59:45
----Original Message----
From: boost-bounces_at_[hidden]
[mailto:boost-bounces_at_[hidden]] On Behalf Of Andy Little Sent:
07 March 2006 15:14 To: boost_at_[hidden]
Subject: Re: [boost] [bitfield] Initial bitfield proposal available
inthevault
> "Martin Bonner" <martin.bonner_at_[hidden]> wrote in message
> news:D997BF79D1E92C4793B7FCC04B4F90A51D79B6_at_pigeon.pi.local...
>> ----Original Message----
>> From: Emile Cormier
>>
>>> The bitfield mechanism relies on this assumption: Unions of
>>> non-polymorphic, non-derived objects, having the exact same
>>> underlying data member type, will have the same size as this
>>> underlying data member type. I'm no language lawyer, so please let
>>> me know if this is a safe and portable assumption.
>>
>> I'm not quite sure what you mean, but given:
>> struct a { unsigned char ch; };
>> struct b { unsigned char ch; };
>> union u { a theA; b theB };
>> then you are not guaranteed that sizeof(u) == sizeof(unsigned char).
>
> Though in practise you can use:
>
> BOOST_STATIC_ASSERT(sizeof(u) == sizeof(unsigned char))
My point was exactly that you CANNOT use that. (On a certain class of
machine).
>
>> On word addressed machines (which /are/ still being built), it is
>> almost certain that the minimum size for a struct is a complete
>> word. This is because the C and C++ standards effectively promise
>> that pointers to structs are all of the same size (the size of a
>> pointer-to-struct does not depend on the contents of the struct).
>> It is desirable that a pointer-to-struct be the smaller,
>> cheaper-to-dereference pointer to word (rather than the larger
>> more-expensive-to-dereference pointer to char), so the smallest
>> struct has to occupy a whole word.
>
> I dont see why the size of a pointer to a struct affects the size of
> a struct which in the case of an empty struct is often 1 byte?
I don't think you have understood what a word addressed machine is!
On most modern archictectures there are 8 bits stored at (for example)
0x100 and another 8 bits at 0x101. The 32 bits at 0x100 cover 0x100,
0x101, 0x102, and 0x103.
On a word addressed machine, there may be 36 bits stored at 02000 and
another (different) 36 bits stored at 02001. A simple 36-bit pointer
can address individual words, but not sub-units within those words. To
address individual bytes, you need a double-word pointer. One word
identifies the word, and a few bits within the second word identifies
which byte you are addressing.
On such a machine, it makes sense for an empty struct to occupy a whole
word (which is four nine-bit bytes), so that a pointer to struct can
(always) be a single word pointer.
-- Martin Bonner Martin.Bonner_at_[hidden] Pi Technology, Milton Hall, Ely Road, Milton, Cambridge, CB4 6WZ, ENGLAND Tel: +44 (0)1223 203894
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk