473,785 Members | 3,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unions and structures implementation in C

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.
Nov 15 '05 #1
4 1780

"Jason Curl" <j_********@mot orola.com> wrote in message
news:di******** **@engnntp2.cig .mot.com...
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?
Nov 15 '05 #2
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
es*****@acm-dot-org.invalid
Nov 15 '05 #3
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.
Nov 15 '05 #4
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
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
13386
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
9
3095
by: Neil Zanella | last post by:
Hello, Some programmers like to use a coding convention where all names of variables that are pointers start with the letter p (and sometimes even use similar conventions for strings and other numeric data types). Do such programmers also have conventions for naming structures and unions (e.g. do they like their names to begin with s and u, so that, when the . operator is used, it is clear whether a structure or union is being accessed...
23
2837
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find relevance.Though both of these(bitfields and unions) are used where space is a constraint(so I can assume always in embedded systems,where memory is particularly less)and we want to save space/memory. As far as I have read, there is no...
13
4136
by: luke | last post by:
hi, in Visula C++ 6.0 I have declared a struct like this: typedef struct _WRITE_INPUT { ULONG DeviceNumber; ULONG RegisterNumber; union { USHORT ShortData; UCHAR CharData;
3
4781
by: Hallvard B Furuseth | last post by:
I'm getting horribly lost in the strict aliasing rules. Is this code correct? struct A { int x; }; struct B { int x, y; }; int foo( struct A *a ) { struct B *b = (struct B *) a; return b->x - b->y; }
67
3374
by: bluejack | last post by:
A recent post asking for help with unions reminded me of this component of the C language that I have only used a couple of times, and those almost entirely out of personal whim -- Unions for the sake of Unions simply because I wanted to see one in action. Granted: it makes it possible to save a few bytes of storage when you have something that can be a chicken or a rooster, but not both, and you're always going to know which it is. ...
26
1929
by: Old Wolf | last post by:
Ok, we've had two long and haphazard threads about unions recently, and I still don't feel any closer to certainty about what is permitted and what isn't. The other thread topics were "Real Life Unions" and "union {unsigned char u; ...} ". Here's a concrete example: #include <stdio.h> int main(void)
11
2025
by: pereges | last post by:
Hello, can some one please guide me a little into using unions. I read about unions in K & R but I am finding it difficult to apply to my problem at hand. I want to save up some space by using unions . My questions are : 1. Is it dangerous to use unions ? Is it worth the trouble if I want to save memory ? Are they error prone ? 2. I read that it is not possible to access more than 1 member at any instant from a union. What does this...
6
3696
by: Ravikiran | last post by:
Hi, I want know the differences between Unions and Structures in C programming. Thank you..
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10327
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10151
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.