| View previous topic :: View next topic |
| Author |
Message |
David Guest
|
Posted: Tue Jun 15, 2004 1:19 pm Post subject: Unions vs Classes |
|
|
Some developers in my group are using UNIONS to define their data
types in a C++ program for an embedded system. Are there any pro and
cons in doing this when you can define a CLASS to do the same thing?
I guess there might be some additional overhead with CLASSES, but is
that really an issue with today's computers?
|
|
| Back to top |
|
 |
Karl Heinz Buchegger Guest
|
Posted: Tue Jun 15, 2004 1:38 pm Post subject: Re: Unions vs Classes |
|
|
David wrote:
| Quote: |
Some developers in my group are using UNIONS to define their data
types in a C++ program for an embedded system. Are there any pro and
cons in doing this when you can define a CLASS to do the same thing?
|
'union' and 'class' do different things.
| Quote: | I guess there might be some additional overhead with CLASSES, but is
that really an issue with today's computers?
|
For todays desktop systems the memory savings by using a union
is seldome an issue. But embedded system are notorious short
on memory.
--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]
|
|
| Back to top |
|
 |
JKop Guest
|
Posted: Tue Jun 15, 2004 2:59 pm Post subject: Re: Unions vs Classes |
|
|
David posted:
| Quote: | Some developers in my group are using UNIONS to define their data
types in a C++ program for an embedded system. Are there any pro and
cons in doing this when you can define a CLASS to do the same thing?
I guess there might be some additional overhead with CLASSES, but is
that really an issue with today's computers?
|
Let's say that
int = 32 bits
char = 8 bits
short = 16 bits
Take the following:
struct MonkeyStruct
{
int a;
char b;
short c;
};
struct MonkeyUnion
{
int a;
char b;
short c;
};
sizeof(MonkeyStruct) will be 56 bits.
sizeof(MonkeyUnion) will be 32 bits.
You can't simply just turn a class into a union or vice-versa! They're two
separate types of entity. Why would a union be in any way superior to a
struct? I myself can only think of two possible reasons:
A) Conserve memory
B) Neat memory tricks (like using a 4 char union to determine if a system is
LITTLEENDIN or BIGENDIN)
-JKop
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Tue Jun 15, 2004 4:27 pm Post subject: Re: Unions vs Classes |
|
|
"David" <mail (AT) david-eng (DOT) com> wrote in message
| Quote: | I guess there might be some additional overhead with CLASSES, but is
that really an issue with today's computers?
|
Depends on the memory. With a union you could reduce the sizeof by 25% or
50% or more. If you create many such classes, then the savings add up. On
the other hand, if you need a type field, you might be better off with
polymorphic classes (ie. virtual functions) as the virtual pointer is itself
a type field.
|
|
| Back to top |
|
 |
Xenos Guest
|
Posted: Tue Jun 15, 2004 7:46 pm Post subject: Re: Unions vs Classes |
|
|
"JKop" <NULL (AT) NULL (DOT) NULL> wrote
| Quote: | David posted:
Let's say that
int = 32 bits
char = 8 bits
short = 16 bits
Take the following:
struct MonkeyStruct
{
int a;
char b;
short c;
};
struct MonkeyUnion
{
int a;
char b;
short c;
};
sizeof(MonkeyStruct) will be 56 bits.
|
Though its implemetation specific, this will most likely be 64, because a 16
short will probably require an alignment of mod 2.
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Tue Jun 15, 2004 8:56 pm Post subject: Re: Unions vs Classes |
|
|
Xenos wrote:
| Quote: | "JKop" <NULL (AT) NULL (DOT) NULL> wrote in message
news:99Ezc.2285$Z14.2284 (AT) news (DOT) indigo.ie...
David posted:
Let's say that
int = 32 bits
char = 8 bits
short = 16 bits
Take the following:
struct MonkeyStruct
{
int a;
char b;
short c;
};
struct MonkeyUnion
{
int a;
char b;
short c;
};
sizeof(MonkeyStruct) will be 56 bits.
Though its implemetation specific, this will most likely be 64,
because a 16 short will probably require an alignment of mod 2.
|
Even without the char, the size would probably be 64 bits, because the
the 4 byte int would need be aligned on a 4 byte boundary, and you have
to remember that the struct must also be usable in an array.
|
|
| Back to top |
|
 |
puppet_sock@hotmail.com Guest
|
Posted: Tue Jun 15, 2004 9:57 pm Post subject: Re: Unions vs Classes |
|
|
JKop <NULL (AT) NULL (DOT) NULL> wrote
[snip]
| Quote: | Why would a union be in any way superior to a
struct? I myself can only think of two possible reasons:
A) Conserve memory
B) Neat memory tricks (like using a 4 char union to determine if a system is
LITTLEENDIN or BIGENDIN)
|
Are you intending to do something like put an integer in and
see what order the bytes come back out?
I'm doubting this last would be safe. You should not count on
how a compiler is going to squash things together in a union.
Socks
|
|
| Back to top |
|
 |
David Rubin Guest
|
Posted: Wed Jun 16, 2004 2:09 am Post subject: Re: Unions vs Classes |
|
|
[email]puppet_sock (AT) hotmail (DOT) com[/email] wrote in message news:<c7976c46.0406151357.2b6b8569 (AT) posting (DOT) google.com>...
| Quote: | JKop <NULL (AT) NULL (DOT) NULL> wrote
[snip]
Why would a union be in any way superior to a
struct? I myself can only think of two possible reasons:
A) Conserve memory
B) Neat memory tricks (like using a 4 char union to determine if a system is
LITTLEENDIN or BIGENDIN)
Are you intending to do something like put an integer in and
see what order the bytes come back out?
|
Given
union Int2Bytes{
int i;
char b[sizeof(int)];
};
I know you cannot write to .i and read from .b, but is it legal to do this?
int i = 0xaabbccdd;
char b = ((Int2Bytes*)&i)->b[0];
/david
|
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Wed Jun 16, 2004 3:34 am Post subject: Re: Unions vs Classes |
|
|
On 15 Jun 2004 06:19:17 -0700, [email]mail (AT) david-eng (DOT) com[/email] (David) wrote in
comp.lang.c++:
| Quote: | Some developers in my group are using UNIONS to define their data
types in a C++ program for an embedded system. Are there any pro and
cons in doing this when you can define a CLASS to do the same thing?
I guess there might be some additional overhead with CLASSES, but is
that really an issue with today's computers?
|
What do "today's computers" have to do with the embedded system?
It might make a vast difference if the target platform is an 8051 core
with 8K 8-bit bytes of code space and 128 (not K) 8-bit bytes of data
space on the one hand, and an ARM or PowerPC with many megabytes of
both.
Embedded systems vary so widely and by so many orders of magnitude
that the difference between common desk top computers and their
operating systems _almost_ seems trivial by comparison.
What do the developers who prefer unions state is the reason for
selecting them over classes?
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Wed Jun 16, 2004 4:09 am Post subject: Re: Unions vs Classes |
|
|
"Jack Klein" <jackklein (AT) spamcop (DOT) net> wrote in message
| Quote: | What do the developers who prefer unions state is the reason for
selecting them over classes?
|
I've heard it's used extensively in low level programming. Also, if you
want to write an API and works in both C++ and C, then unions might be
right, as in the sockets class.
|
|
| Back to top |
|
 |
Chris Hill Guest
|
Posted: Wed Jun 16, 2004 1:03 pm Post subject: Re: Unions vs Classes |
|
|
On Wed, 16 Jun 2004 04:09:02 GMT, "Siemel Naran"
<SiemelNaran (AT) REMOVE (DOT) att.net> wrote:
| Quote: | "Jack Klein" <jackklein (AT) spamcop (DOT) net> wrote in message
What do the developers who prefer unions state is the reason for
selecting them over classes?
I've heard it's used extensively in low level programming. Also, if you
want to write an API and works in both C++ and C, then unions might be
right, as in the sockets class.
|
I have seen the situation of auto generate interface code that used
structs to hold different data and then collected these together
using unions and a type field. The client would inspect the type
field and then delve into the correct bit of the struct/union
combination.
This was actually consuming more space than a class hierarchy
and proper polymorphism. Since, the unions were as large as
the largest member. I suspect that paying the price of VPTRs
and using a class hierarchy (even with multiple inheritance) would
consume less space.
:) Chris.
|
|
| Back to top |
|
 |
JKop Guest
|
Posted: Wed Jun 16, 2004 1:25 pm Post subject: Re: Unions vs Classes |
|
|
posted:
| Quote: | JKop <NULL (AT) NULL (DOT) NULL> wrote in message
news:<99Ezc.2285$Z14.2284 (AT) news (DOT) indigo.ie>... [snip]
Why would a union be in any way superior to a
struct? I myself can only think of two possible reasons:
A) Conserve memory
B) Neat memory tricks (like using a 4 char union to determine if a
system is LITTLEENDIN or BIGENDIN)
Are you intending to do something like put an integer in and
see what order the bytes come back out?
I'm doubting this last would be safe. You should not count on
how a compiler is going to squash things together in a union.
Socks
|
union {
unsigned int FourBytes;
struct {
unsigned char Byte1;
unsigned char Byte2;
unsigned char Byte3;
unsigned char Byte4;
};
} blind;
int main(void)
{
blind.FourBytes = 0x1F1F1F1F;
//Now, check each individual byte
if (Byte1 ==
}
I did this before in a program I was writing. Can't remember why I did it,
or which numbers I used! I'd still have it tucked away in my archive though
(Actually, just right now as I'm writing this I thought of the reason, I was
encoding digital audio to be sent down to a CD-Writer, and had to know the
ENDINESS).
-JKop
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Wed Jun 16, 2004 4:57 pm Post subject: Re: Unions vs Classes |
|
|
"Chris Hill" <news1 (AT) plantain (DOT) org.uk> wrote in message
| Quote: | This was actually consuming more space than a class hierarchy
and proper polymorphism. Since, the unions were as large as
the largest member. I suspect that paying the price of VPTRs
and using a class hierarchy (even with multiple inheritance) would
consume less space.
|
Furthermore the VPTR way is probably faster than a switch-case on the type
field.
|
|
| Back to top |
|
 |
Bill Seurer Guest
|
Posted: Wed Jun 16, 2004 8:08 pm Post subject: Re: Unions vs Classes |
|
|
JKop wrote:
| Quote: | union {
unsigned int FourBytes;
struct {
unsigned char Byte1;
unsigned char Byte2;
unsigned char Byte3;
unsigned char Byte4;
};
} blind;
int main(void)
{
blind.FourBytes = 0x1F1F1F1F;
//Now, check each individual byte
if (Byte1 ==
I did this before in a program I was writing. ...
|
While using unions is generally machine specific anyway the above is
especially "bad" because it makes many assumptions (like ints are 4 bytes).
|
|
| Back to top |
|
 |
puppet_sock@hotmail.com Guest
|
Posted: Tue Jun 29, 2004 5:53 pm Post subject: Re: Unions vs Classes |
|
|
[email]davidrubin (AT) warpmail (DOT) net[/email] (David Rubin) wrote in message news:<82b37be.0406151809.13116930 (AT) posting (DOT) google.com>...
[puppet sock moaning about in-one-var-out-the-other unions snipped]
| Quote: | Given
union Int2Bytes{
int i;
char b[sizeof(int)];
};
I know you cannot write to .i and read from .b,
|
You can, it's just that it is not portable, and probably even
not reliable. Some things that could bust it:
- Different hardware
- Different OS (even new version)
- Different version of the compiler
- Different address of loading the code into memory
- Long list of weird things that could happen on specialist platforms
| Quote: | but is it legal to do this?
int i = 0xaabbccdd;
char b = ((Int2Bytes*)&i)->b[0];
|
I'm pretty confused by that. What's it supposed to do?
Lessee, you've got the address of an int variable, and
you've cast that as a pointer to an undeclared union,
and you've tried to reference the zeroeth char in the
"b" data element of that undeclared union. I'd say,
that this *might* compile, but the results won't be good.
The best you could hope for would be some kind of memory
access violation. The worst would be that char b contains
garbage and you don't realize it.
Socks
|
|
| Back to top |
|
 |
|