Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Bytes' orde...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 21 of 30 Topic 9591 of 10104
Post > Topic >>

Re: Bytes' order with different C++ compilers

by "C. M. Heard" <nobody@[EMAIL PROTECTED] > May 21, 2008 at 01:28 AM

On Tue, 20 May 2008 05:14:57 CST, Martin Bonner <martinfro...@[EMAIL PROTECTED]
>
wrote:
> On May 19, 6:59 pm, nickf3 <nic...@[EMAIL PROTECTED]
> wrote:
> > On May 18, 1:21 am, Martin Bonner <martinfro...@[EMAIL PROTECTED]
> wrote:
> > > On May 17, 9:29 am, nickf3 <nic...@[EMAIL PROTECTED]
> wrote:
> > > > On May 16, 8:27 pm, Alan McKenney <alan_mckenn...@[EMAIL PROTECTED]
>
wrote:
> > > > Please don't reinvent the wheel, use standard library:
> > > >       #include <arpa/inet.h>
> > > >       uint32_t
> > > >       htonl(uint32_t hostlong);
> [etc]
> > > > These are noops on big-endian machines, so there's no overhead.

As I argued in an earlier post, that makes them easy to inadvertently
omit when one develops and tests code on a big endian machine, as I have
found out the hard way (more than once) when ****ting open-source code
developed on a Sparc to an x86.

[ ... ]
> > Let's look at the (open) source, shall we:
> >
> [snip]
> > u_int32_t
> > ntohl(u_int32_t x)
> > {
> > #if BYTE_ORDER == LITTLE_ENDIAN
> >    u_char *s = (u_char *)&x;
> >    return (u_int32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]);
> > #else
> >         return x;
> > #endif
> > }
> >
> > Taken directly from OpenBSD cvs. Looks familiar, doesn't it?
> > Where's that horror you speak of?
[ ... ]
> ... however the real horror is the interface.  It takes a u_int32_t in
> and returns a value of the same type.  This is plain wrong.  Depending
> one which way you are going, the conversion needs to be between a
> u_int32_t and an array of four unsigned char (which can hold an
> octet).

Exactly so.

> [ ... ] sane communications libraries deal in sequences of
> octets stored in unsigned char (which are then packed up in the
> correct order).  ntohl is based on an approach where you store bytes
> into the memory of a struct, and then fix-up the byte order.  It
> doesn't work if your platform has CHAR_BIT == 64; it requires you to
> control padding between member variables of a struct; it requires you
> to declare the structure with specific-sized members (rather than a
> natural "int" or "long").

Right.  It is _never_ a good idea to attempt to overlay a struct (or
a class) on top of a sequence of octets received from (or to be sent
to) a communication interface, as the classic BSD libraries do.  Too
many things can go wrong, even when there are only issues with byte
order.  It is much more foolproof to represent the content of the
packet/frame/datagram headers in well-designed structs or cl*****
and do the conversions to and from octet sequences explicitly.

C. M. Heard

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 30 Posts in Topic:
Bytes' order with different C++ compilers
Ulysses4ever@[EMAIL PROTE  2008-05-11 16:40:44 
Re: Bytes' order with different C++ compilers
Carl Barron <cbarron41  2008-05-11 21:59:54 
Re: Bytes' order with different C++ compilers
Francis Glassborow <fr  2008-05-13 10:56:10 
Re: Bytes' order with different C++ compilers
Tony Delroy <tony_in_d  2008-05-13 21:14:12 
Re: Bytes' order with different C++ compilers
ThosRTanner <ttanner2@  2008-05-14 15:13:31 
Re: Bytes' order with different C++ compilers
Ulysses4ever@[EMAIL PROTE  2008-05-14 15:13:17 
Re: Bytes' order with different C++ compilers
Francis Glassborow <fr  2008-05-14 18:10:42 
Re: Bytes' order with different C++ compilers
Alan McKenney <alan_mc  2008-05-15 11:17:54 
Re: Bytes' order with different C++ compilers
Bart van Ingen Schenau &l  2008-05-15 16:04:27 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-16 17:48:11 
Re: Bytes' order with different C++ compilers
Alan McKenney <alan_mc  2008-05-16 18:27:29 
Re: Bytes' order with different C++ compilers
nickf3 <nickf3@[EMAIL   2008-05-17 02:29:20 
Re: Bytes' order with different C++ compilers
Bart van Ingen Schenau &l  2008-05-17 14:35:47 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-17 23:21:56 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-17 23:21:45 
Re: Bytes' order with different C++ compilers
Greg Herlihy <greghe@[  2008-05-19 06:05:51 
Re: Bytes' order with different C++ compilers
nickf3 <nickf3@[EMAIL   2008-05-19 11:59:02 
Re: Bytes' order with different C++ compilers
Alan McKenney <alan_mc  2008-05-19 12:00:38 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-20 05:14:57 
Re: Bytes' order with different C++ compilers
nickf3 <nickf3@[EMAIL   2008-05-21 01:22:18 
Re: Bytes' order with different C++ compilers
"C. M. Heard" &  2008-05-21 01:28:17 
Re: Bytes' order with different C++ compilers
Alan McKenney <alan_mc  2008-05-21 14:03:42 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-21 14:01:51 
Re: Bytes' order with different C++ compilers
Bart van Ingen Schenau &l  2008-05-21 14:27:32 
Re: Bytes' order with different C++ compilers
nickf3 <nickf3@[EMAIL   2008-05-22 01:14:57 
Re: Bytes' order with different C++ compilers
nickf3 <nickf3@[EMAIL   2008-05-22 14:58:37 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-24 09:28:08 
Re: Bytes' order with different C++ compilers
Gerhard Fiedler <gelis  2008-05-25 09:57:35 
Re: Bytes' order with different C++ compilers
Martin Bonner <martinf  2008-05-26 16:16:35 
Re: Bytes' order with different C++ compilers
Gerhard Fiedler <gelis  2008-05-28 10:41:03 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Oct 15 22:33:30 CDT 2008.