comp.lang.ada
 help / color / mirror / Atom feed
From: rooi@zen.et.tudelft.nl (Mark de Rooi)
Subject: Re: Ada95 - Determining Machine Byte Order
Date: 8 Feb 1995 12:36:51 GMT
Date: 1995-02-08T12:36:51+00:00	[thread overview]
Message-ID: <3hadt3$44p@liberator.et.tudelft.nl> (raw)
In-Reply-To: 3h45bq$fev@gnat.cs.nyu.edu

dewar@cs.nyu.edu (Robert Dewar) writes:

>Not only is Mark's solution:

>        int n = 1, little_endian = (*((char *)&n) == n);

>not very clear (though if you study it for a second and can find your
>way around * and & in C, you can see how it works), but more significantly
>there is no guarantee that this will work, it is equivalent to the use of
>unchecked conversion in Ada, and equally problematic. Mark if you don't
>believe me, try seeing what this gives on a Cray, or if you want to take
>the theoretical approach, tell me where in the ANSI standard it is written
>that this will do something well defined.

Unfortunately, I don't have a Cray handy, but I will take your word for
it that my suggestion won't work on one. As I mentioned in my followup,
it is required that sizeof(char) < sizeof(int) for this to work.
Is sizeof(char) == sizeof(int) for the compiler you use on your Cray?
I am curious as to the reason my (admittedly not very clear) solution
doesn't work on a Cray.

As for the ANSI C standard, well, _of course_ casting a pointer-to-int
to a pointer-to-char and then dereferencing it is machine- and compiler-
dependent. If it weren't, there would be no point to my obscure piece of
code!

Note that I specifically mentioned that it wouldn't work on *every*
machine. It works on every PC, Mac and Unix workstation I ever used,
but not on some DSPs, and apparently, on a Cray. I wasn't aware of the
Ada Way (tm) to handle this, so I just gave a quick fix to the guy who
asked, so he could get on with his work.


>All in all I think that the Ada way of doing things:

>   Little_Endian : constant Boolean :=
>        System.Default_Bit_Order = System.Low_Order_First;

>maybe just a *little* bit clearer :-)

No argument there ;-)

Regards,

Mark





      reply	other threads:[~1995-02-08 12:36 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-02-02  4:47 Ada95 - Determining Machine Byte Order Timothy Halloran
     [not found] ` <3gvudv$k4b@liberator.et.tudelft.nl>
1995-02-06  3:34   ` Robert Dewar
1995-02-08 12:36     ` Mark de Rooi [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox