From 5100760625833775586
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,738ae3371beb3408
X-Google-Attributes: gidf78e5,public
From: "C. M. Heard/VVNET, Inc." <heard@vvnet.com>
Subject: Re: portable method of determining endianness byte order.
Date: 1999/11/22
Message-ID: <3835c8a9$0$209@nntp1.ba.best.com>#1/1
X-Deja-AN: 551444366
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
References: <38329967.470B6CED@hqs.mid.gmeds.com> <m5PMyMAN8aN4EwXX@robinton.demon.co.uk>
X-Original-Date: 19 Nov 1999 22:01:13 GMT
X-Complaints-To: news@news.unimelb.edu.au
X-Trace: ariel.ucs.unimelb.edu.au 943232143 6087 128.250.37.153 (22 Nov 1999 00:55:43 GMT)
Organization: -
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUAODiUkuEDnX0m9pzZAQGnPQGAl2evYDzGvCPP5XlbJbviznux+/VceM0V rc+TjWK8ewtVrOW7PKRWMViHr33YdlCu =xI8P
NNTP-Posting-Date: 22 Nov 1999 00:55:43 GMT
Newsgroups: comp.std.c++

Pukalo Boyd wrote:
> I have been trying to find a portable method of determining the
> endianness byte order at compile time
> to produce code that will work properly on both windows and unix
> machines. I have searched the documentation
> i have ( the books on the C Standard Library and the C++ Standard
> Library) and havent found anything. Please help. Any pointers or
> suggestions would be very helpful. Thanks.

While this is possible (as Francis Glassborow pointed out), in my
experience code that is designed to work properly regardless of
host byte-order is a lot less prone to error.

One specific example:  I've worked on IP protocol modules written in
the traditional Berkeley fashion, which is to convert multibyte fields
in packet headers from network to host order (and vice-versa) by
explicitly invoking the macros ntohs/ntohl/htons/htonl.  It's very easy
to forget to invoke one of the macros where it is needed, resulting in
code that tends to be fragile.  Errors are particularly likely to creep
in when one makes modifications on a host where the macros don't do
anything;  one does not notice the errors until the code is recompiled
for a host of the opposite byte order.  Most such problems disappear
when conversions are done by explicit masking and shifting, since
correctness is not dependent on host byte order.

I'd strongly advise you to reconsider your decision to write code that
needs to know the host byte order.

Mike
--
C. M. Heard/VVNET, Inc.
heard@vvnet.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]



