Connecting Tech Pros Worldwide Forums | Help | Site Map

Endianness

kelvSYC
Guest
 
Posts: n/a
#1: Jul 23 '05
Are there any endianness concerns in C++, or does the compiler take
care of those details? I ask because I'm not sure if code such as the
following have consistent behavior on all platforms.

typedef unsigned int u32; // sizeof(int) == 4
typedef unsigned char u8;

u8 array[4] = { 0x01, 0x23, 0x45, 0x67 };
*((u32*) array) = 0x89ABCDEF;

Will array contain { 0x89, 0xAB, 0xCD, 0xEF } or { 0xEF, 0xCD, 0xAB,
0x89 }, or even something else?

--
I am only a mirage.

John Carson
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Endianness


"kelvSYC" <kelvSYC@no.email.shaw.ca> wrote in message
news:030620050819458929%kelvSYC@no.email.shaw.ca[color=blue]
> Are there any endianness concerns in C++, or does the compiler take
> care of those details?[/color]

Sure, as long as you don't do silly things like cast between an array of
chars and an int.
[color=blue]
> I ask because I'm not sure if code such as the
> following have consistent behavior on all platforms.
>
> typedef unsigned int u32; // sizeof(int) == 4
> typedef unsigned char u8;
>
> u8 array[4] = { 0x01, 0x23, 0x45, 0x67 };
> *((u32*) array) = 0x89ABCDEF;
>
> Will array contain { 0x89, 0xAB, 0xCD, 0xEF } or { 0xEF, 0xCD, 0xAB,
> 0x89 }, or even something else?[/color]

Depends on endianness.

--
John Carson

Alf P. Steinbach
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Endianness


* kelvSYC:[color=blue]
> Are there any endianness concerns in C++,[/color]

Yes.

[color=blue]
> or does the compiler take care of those details?[/color]

Except for seralization to/from byte stream it's easy to code such
that endianness doesn't matter.

AFAIK the language offers no support for endian-specific code.

std::numeric_limits<T> can tell you whether an integer type is an integer
type, whether it's got modulo arithemetic, and whether it's signed or
not, but it doesn't tell you anything about endianness.

[color=blue]
> I ask because I'm not sure if code such as the
> following have consistent behavior on all platforms.[/color]

It doesn't.

[color=blue]
> typedef unsigned int u32; // sizeof(int) == 4
> typedef unsigned char u8;
>
> u8 array[4] = { 0x01, 0x23, 0x45, 0x67 };
> *((u32*) array) = 0x89ABCDEF;
>
> Will array contain { 0x89, 0xAB, 0xCD, 0xEF } or { 0xEF, 0xCD, 0xAB,
> 0x89 }, or even something else?[/color]

As a practical matter the result depends on the endianness, as you've
surmised.

As a formal matter the camouflaged reinterpret_cast in there makes it
all either UB or implementation defined, I'm not sure which.

Anyway, all you have to do to avoid that unpleasantness is to NOT USE
REINTERPRETATION CASTS, and that includes not using C-style casts. ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Donovan Rebbechi
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Endianness


On 2005-06-03, kelvSYC <kelvSYC@no.email.shaw.ca> wrote:[color=blue]
> Are there any endianness concerns in C++,[/color]

As others have pointed out, only if you write endian-dependent code.

The only reason I can think of to write endian-dependent code is as another
poster pointed out, if you're trying to serialize data. If you do this, you
need to have a consistent policy about endianness and byte representation.
[color=blue]
> or does the compiler take care of those details?[/color]

No. You need some sort of preliminary configuration to detect endianness.
[color=blue]
> I ask because I'm not sure if code such as the
> following have consistent behavior on all platforms.[/color]

No. You can use similar code to test endianness. BTW, you also need to be
careful with different size ints.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Closed Thread