Boost logo

Boost :

From: Martin Bonner (martin.bonner_at_[hidden])
Date: 2005-08-11 08:22:35


----Original Message----
From: christopher diggins [mailto:cdiggins_at_[hidden]]
Sent: 11 August 2005 14:00
To: boost_at_[hidden]
Subject: Re: [boost] alignment problem in proposed any alternative

[snip]

> 3.9(2) says:
> For any complete POD object type T, whether or not the object holds a
> valid value of type T, the underlying
> bytes (1.7) making up the object can be copied into an array of char
> or unsigned char.36) If the content of the array of char or unsigned
> char is copied back into the object, the object shall subsequently
> hold its original value.
>
> This has nothing to do with void* and char*, but rather with all
> POD's.
>
> This clearly implies char arrays are stringently aligned.

Err, no it doesn't. It says that you can store the bits of a POD object
into a char array, and then load the bits back into the same (correctly
aligned) POD object.

It does *NOT* say that you can use the address of the char array as if it
were the address of the POD object.

For example:
#include <assert.h>
int main()
{
        long pod = 3141L;
        char pad;
        char c[sizeof(pod)];
        memcpy( c, &pod, sizeof(pod) );
        pod = 42L;
        memcpy( &pod, c, sizeof(pod) );
        assert (pod == 3141L); // 3.9(2) requires this assert to pass.
        // nothing requires THIS assert to pass, and on some systems it fail
        // because c is not suitable aligned.
        assert ( *reinterpret_cast<long*>(&c[0]) == 3141L );
      return 0;
}

-- 
Martin Bonner
Martin.Bonner_at_[hidden]
Pi Technology, Milton Hall, Ely Road, Milton, Cambridge, CB4 6WZ,
ENGLAND Tel: +44 (0)1223 441434

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