Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Unions and structures implementation in C

Reply
Thread Tools

Unions and structures implementation in C

 
 
Jason Curl
Guest
Posts: n/a
 
      10-12-2005
Hello,

Does the C standard specify (and if so, how, or possibly a reference to
the standard - I don't have a copy). I am also assuming that CHAR_BIT is
8 (for Posix systems)

a) How elements in a union overlap
b) limitations on how elements in a structure are padded

This will help me in writing (hopefully) portable C code that can
communicate across different architectures, that may be faster and more
memory efficient than copying from one buffer to another.

Or is the only practical way to memcpy() taking into account endianness
of the machine into a "char *buffer"?

Thanks,
Jason.
 
Reply With Quote
 
 
 
 
pemo
Guest
Posts: n/a
 
      10-12-2005

"Jason Curl" <(E-Mail Removed)> wrote in message
news:diidc8$4ru$(E-Mail Removed)...
> Hello,
>
> Does the C standard specify (and if so, how, or possibly a reference to
> the standard - I don't have a copy). I am also assuming that CHAR_BIT is 8
> (for Posix systems)
>
> a) How elements in a union overlap
> b) limitations on how elements in a structure are padded
>
> This will help me in writing (hopefully) portable C code that can
> communicate across different architectures, that may be faster and more
> memory efficient than copying from one buffer to another.
>
> Or is the only practical way to memcpy() taking into account endianness of
> the machine into a "char *buffer"?


You can get a copy of the c99 std from the ANSI website for $18 - might be
worth investing?


 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      10-12-2005
Jason Curl wrote:
> Hello,
>
> Does the C standard specify (and if so, how, or possibly a reference to
> the standard - I don't have a copy). I am also assuming that CHAR_BIT is
> 8 (for Posix systems)
>
> a) How elements in a union overlap


All the union's elements start at the same address.
More formally, a pointer to the union can be converted
to a pointer to any of its elements, and vice versa.

> b) limitations on how elements in a structure are padded


Padding may appear after any element of the struct.
(People often say "Between any elements, and at the end
of the struct," but that's just a longer way of saying
the same thing.) The struct's first element starts at
the struct's address; more formally, a pointer to the
struct can be converted to a pointer to its first element,
and vice versa.

> This will help me in writing (hopefully) portable C code that can
> communicate across different architectures, that may be faster and more
> memory efficient than copying from one buffer to another.
>
> Or is the only practical way to memcpy() taking into account endianness
> of the machine into a "char *buffer"?


memcpy() might not be enough. For example, you may
want to communicate a `long' value, but `long' on machine A
is 32 bits while on machine B it's 64. Or perhaps you want
to move a `long double' value between a machine were it is
actually more precise than plain `double' and a machine
where it's really just a plain `double' with a different name.
Mere memcpy() cannot reconcile such differences.

What's usually needed is a careful specification of the
external format -- the way the information looks "on the
wire" or "in the file." Then for each machine you write a
pair of functions (or a set of pairs): one to convert the
machine's idiosyncratic internal data representation to the
external form, and one to digest the external form and convert
it to the internal representation. A visit to someplace like
wotsit.org may give you some ideas about how to specify an
external format that has a reasonable chance at portability:
study the way JFIF or WAV files (for example) are described,
and see if you can steal, er, "be inspired by" their techniques.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      10-12-2005
Jason Curl a écrit :
> Does the C standard specify (and if so, how, or possibly a reference to
> the standard - I don't have a copy). I am also assuming that CHAR_BIT is
> 8 (for Posix systems)
>
> a) How elements in a union overlap
> b) limitations on how elements in a structure are padded


Are your facing such questions probably means that you have a design
error. Unions are used to save memory, but certainely not to do parallel
memory mappings.

Arrays of unsigned char and bitwise operators are your friends.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      10-12-2005
Jason Curl wrote:

> Hello,
>
> Does the C standard specify (and if so, how, or possibly a reference
> to the standard - I don't have a copy). I am also assuming that
> CHAR_BIT is 8 (for Posix systems)
>
> a) How elements in a union overlap
> b) limitations on how elements in a structure are padded
>
> This will help me in writing (hopefully) portable C code that can
> communicate across different architectures, that may be faster and
> more memory efficient than copying from one buffer to another.
>
> Or is the only practical way to memcpy() taking into account
> endianness of the machine into a "char *buffer"?



There's no way you're going to achieve what you want with unions and
memcpy and such. Cross-platform communication has many potential traps
and pitfalls. Besides structure padding, there are issues with size and
endianess of basic data types.

Look up the topic of serialization. You will find much prior art on the
subject. The FAQ from another newsgroup, comp.lang.c++ has some
information.


http://www.parashift.com/c++-faq-lit....html#faq-36.1



Brian
 
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
Difference between Unions and Structures... Ravikiran C Programming 7 11-03-2008 10:48 PM
Difference between Unions and Structures... Ravikiran C Programming 2 11-03-2008 11:29 AM
byte alignment in structures and unions anon.asdf@gmail.com C Programming 20 08-10-2007 04:13 PM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
coding conventions: pointers, structures, and unions Neil Zanella C Programming 9 10-16-2003 02:47 AM



Advertisments