[Numpy-discussion] Changeset 6557

David Cournapeau cournape at gmail.com
Sat Mar 7 12:41:20 EST 2009


On Sat, Mar 7, 2009 at 6:01 AM, Charles R Harris
<charlesr.harris at gmail.com> wrote:
> Hi David,
>
> Currently,
>
> bint.i = __STR2INTCST("ABCD");
>
> It is probably more portable to just initialize the union
>
>     union {
>         char c[4];
>         npy_uint32 i;
>     } bint = {'A','B','C','D'};
>

Ah, tempting, right ? It does not work. It has exactly the same
problem as multibyte initialization, that is it is undefined, or at
least there are some platforms where depending on the compiler, the
result will be different.

Mac OS X makes this easy to test (on x86). With your initialization
scheme, bint.c[0] is 'A' whether I compile with -arch x86 or -arch ppc
(mac os x can run ppc code in intel thanks to rosetta, a JIT ppc vm).
With mine, it does what is expected ('A' on big endian - ppc, and 'D'
on little endian).

cheers,

David



More information about the NumPy-Discussion mailing list