Good use for Unions

M

Michael

Hi guys I've been programming C++ for quite some time, but I have never used
unions. I am trying to find a good exmaple of when they might be used. can
anyone provide one??
Regards

Michael.
 
V

Victor Bazarov

Michael said:
Hi guys I've been programming C++ for quite some time, but I have never used
unions. I am trying to find a good exmaple of when they might be used. can
anyone provide one??

Look up "VARIANT" data type in Microsoft's OLE and COM documents.

V
 
E

Efrat Regev

I can't think of a very common case. However, in some recursive data
structures (e.g., trees), you might want each conceptual node to be either a
"real" node or a "nill" node. There are many different ways of implementing
such a data structure. Suppose you're *really* concerned about indirection
(i.e., you wish to avoid it). Then you might create a vector whose each
element an object of
struct node
{
bool m_real_node;

union m_true_node
{
real_node m_real_node;

nil_node m_nil_node;
}
};

So that's a case where you might use a union. As far as I understand, the
above (quite ugly) example should be used in very extreme circumstances (if
you're convinced this will actually improve performance), and you should
encapsulate the hell out of it into a container employing such nodes
internally.

By the way, I vaguely recall the above code working ~3 times as fast as a
version I wrote without unions (based on pointers, and using NULL to signify
a "nil" node) (of course, since I wrote them both, who knows what I might
have fudged, also, it's been years, and I can't recall the exact settings).
 
S

shez

Michael said:
Hi guys I've been programming C++ for quite some time, but I have never used
unions. I am trying to find a good exmaple of when they might be used. can
anyone provide one??
Regards

Michael.

Force alignment of a buffer:

union {
char buffer[1000];
int dummy;
};

This forces the compiler to start 'buffer' at an integer boundary.
-shez-
 
H

Hayden A. James

If you're developing a specialized container class which houses builtin
data types as to provide your other interfaces with a common object to
pass around as data. For example;

class Data
{
union
{
char c;
int i;
float f;
};

type getType();
operator char();
operator int();
operator float();
};

et cetera.
 
A

Aslan Kral

haber iletisinde þunlarý said:
Hi guys I've been programming C++ for quite some time, but I have never used
unions. I am trying to find a good exmaple of when they might be used. can
anyone provide one??
Regards

Michael.

I use it for something like this for example.

union unionPw
{
int isPasswordSet;
char szPassword[16];
};

isPasswordSet = 0; // initialize
....
strcpy(szPassword, "password"); // isPasswordSet is set to a non-zero value
 
C

Clark S. Cox III

haber iletisinde þunlarý said:
Hi guys I've been programming C++ for quite some time, but I have never used
unions. I am trying to find a good exmaple of when they might be used. can
anyone provide one??
Regards

Michael.

I use it for something like this for example.

union unionPw
{
int isPasswordSet;
char szPassword[16];
};

isPasswordSet = 0; // initialize
...
strcpy(szPassword, "password"); // isPasswordSet is set to a non-zero value

What happens if the first sizeof(int) bytes of "password" happens to be
a trap representation for int?
 
A

Aslan Kral

haber iletisinde þunlarý said:
I use it for something like this for example.

union unionPw
{
int isPasswordSet;
char szPassword[16];
};

isPasswordSet = 0; // initialize
...
strcpy(szPassword, "password"); // isPasswordSet is set to a non-zero
value

What happens if the first sizeof(int) bytes of "password" happens to be
a trap representation for int?

On Intel for sizeof(int)=4
before strcpy: isPasswordSet = 0
after strcpy: isPasswordSet = 0x73736171 ("pass")

So you can do something like this after strcpy:

if ( isPasswordSet )
{
....
}
else
{
....
}

What do you mean by "trap representation for int"?
 
A

Andre Kostur

haber iletisinde þunlarý said:
I use it for something like this for example.

union unionPw
{
int isPasswordSet;
char szPassword[16];
};

isPasswordSet = 0; // initialize
...
strcpy(szPassword, "password"); // isPasswordSet is set to a
non-zero
value

What happens if the first sizeof(int) bytes of "password" happens to
be a trap representation for int?

On Intel for sizeof(int)=4
before strcpy: isPasswordSet = 0
after strcpy: isPasswordSet = 0x73736171 ("pass")

So you can do something like this after strcpy:

No, you can't. It is undefined behaviour to read from any member of the
union that wasn't the last one that was set. Since the last thing you
set was szPassword, you may not read from isPasswordSet.
if ( isPasswordSet )
{
...
}
else
{
...
}

What do you mean by "trap representation for int"?

There may exist certain bit patterns for an int that represents an
invalid value. By attempting to use it, the CPU (or something else) may
cause some sort of trap causing your application to crash.
 
A

Aslan Kral

haber iletisinde þunlarý said:
There may exist certain bit patterns for an int that represents an
invalid value. By attempting to use it, the CPU (or something else) may
cause some sort of trap causing your application to crash.

How do you think it will be decoded as an opcode by the CPU?
It is just a value in the data segment.
 
A

Andre Kostur

How do you think it will be decoded as an opcode by the CPU?
It is just a value in the data segment.

Doesn't need to be an executable opcode. It is theoretically possible that
on the UberCPU 9000 platform, in order to perform an operation on an int,
it needs to load it into a register. That register may have restrictions
on what constitutes a legal int. When you then load an invalid int
representation into that register, BOOM.
 
A

Aslan Kral

haber iletisinde þunlarý said:
Doesn't need to be an executable opcode. It is theoretically possible that
on the UberCPU 9000 platform, in order to perform an operation on an int,
it needs to load it into a register. That register may have restrictions
on what constitutes a legal int. When you then load an invalid int
representation into that register, BOOM.

Hmm. I didn't know that. The value in my code was defined for a zero test.
Of course things like divide by zero exception or buffer overflow may happen
if you
are not careful. Other than those things I don't see any problem for Intel
platform.
That is not the subject. I was only giving a simple example. I don't it
creates such
problems.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top