Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Endianness of structure/union

Reply
Thread Tools

Endianness of structure/union

 
 
SSM
Guest
Posts: n/a
 
      09-27-2005
Hi,

Does C standard comment about "Endianness" to be used
to store a structure/union variables?

Thanks & Regards,
Mehta


 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      09-27-2005
"SSM" <(E-Mail Removed)> wrote:

> Does C standard comment about "Endianness" to be used
> to store a structure/union variables?


Not if you mean the endianness of the various members themselves. What
it does require is that:
- for a union, all members start at the same address, which is also the
base address of the union;
- for a struct, all non-bitfield members start at different addresses,
with the first declared member having the lowest address, and so on in
order of declaration;
- bit field members have no address as such;
- there may be padding between two struct members and after the last,
but not before the first.

This is similar, but not equal, to endianness.

Richard
 
Reply With Quote
 
 
 
 
Alexei A. Frounze
Guest
Posts: n/a
 
      09-27-2005
"SSM" <(E-Mail Removed)> wrote in message
news:dhbj97$kvo$(E-Mail Removed)...
> Does C standard comment about "Endianness" to be used
> to store a structure/union variables?


AFAIK, no. It can be anything, depending on the CPU for which you compile
your source code.

The only place in an application where endianness would matter is I/O (file
system, network or any other device consuming and producing data in big
chunks). Only there you should care about the endianness if you exchange
data with anything else but your own application.

There's a way to make the application more portable across systems with
different endianness. Send/receive integer numbers octet per octet (note
that by octet I mean an 8-bit integer, which is not necessarily a byte/char
as byte/char has different bits on different systems) in a fully defined
order, either least significant first or most significant first -- pick one
and stick to it. Slice the numbers into octets by shifting and masking --
this will make it independent from the CPU's endianness. It's OK to send the
numbers as text too but that's obviously too much of overhead in many
applications. The raw data, which you don't know and don't process, is to be
sent as-is, preferrably as octets too, best if octet=byte(char), if this
equality doesn't hold (byte/char is bigger than octet), then you must make a
decision as to how to get it out, as octets or as bytes/chars and if as
octets how you slice the date into them (again, least significant first or
most significant first).

Alex


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Endianness and streams kelvSYC C++ 8 06-06-2005 12:08 AM
Endianness kelvSYC C++ 3 06-03-2005 02:49 PM
memcpy() and endianness Case C Programming 26 05-12-2004 02:47 PM
endianness and sscanf/sprintf pramod C++ 22 01-06-2004 01:02 PM
endianness and sscanf/sprintf pramod C Programming 22 01-06-2004 01:02 PM



Advertisments