Newbie question on unions

P

patleonard

The following code produces this output:

Characters: XY
Integer: 22872

Where does the integer 22872 come from?

#include <iostream.h>

int main()
{
union test_union{
short int i;
char ch[2];
} unvar;

unvar.ch[0] = 'X';
unvar.ch[1] = 'Y';

cout << "Characters: " << unvar.ch[0] << unvar.ch[1] << '\n';
cout << "Integer: " << unvar.i << '\n';

return 0;
}

It may be a stupid question, but I don't know the answer. Any response will
be helpful, thank you.
 
R

Ron Natalie

patleonard said:
The following code produces this output:

Characters: XY
Integer: 22872

Where does the integer 22872 come from?

It's the content of unvar.i. All the elements of the
union occupy the same memory. You're looking at the
memory in which you stored 'X' and 'Y' treated as an
shirt int.
 
B

Bugs_Slayer

patleonard said:
The following code produces this output:

Characters: XY
Integer: 22872

Where does the integer 22872 come from?

#include <iostream.h>

int main()
{
union test_union{
short int i;
char ch[2];
} unvar;

unvar.ch[0] = 'X';
unvar.ch[1] = 'Y';

cout << "Characters: " << unvar.ch[0] << unvar.ch[1] << '\n';
cout << "Integer: " << unvar.i << '\n';

return 0;
}

It may be a stupid question, but I don't know the answer. Any response
will
be helpful, thank you.

X is 88 and Y is 89 as ascii code. each is concantenated in memory and
occupy 1byte.

88 is 0x58 and 89 is 0x59. 0x5859 is located in memory. this is 22617 in
decimal number but

we read 0x5859 as 0x5958. that's because little endian. each bytes read form
right to left.

0x5958 is 22872 in decimal number. so output is 22872
 
R

Rolf Magnus

patleonard said:
The following code produces this output:

Characters: XY
Integer: 22872

Where does the integer 22872 come from?

#include <iostream.h>

int main()
{
union test_union{
short int i;
char ch[2];
} unvar;

unvar.ch[0] = 'X';
unvar.ch[1] = 'Y';

cout << "Characters: " << unvar.ch[0] << unvar.ch[1] << '\n';
cout << "Integer: " << unvar.i << '\n';

Reading a member of a union after writing to another one is not allowed in
C++. Most platforms I know will just give you a reinterpretation of the two
char values as one short value, but the right tool to do this is a
reinterpret_cast.
 
R

Ron Natalie

Rolf said:
Reading a member of a union after writing to another one is not allowed in
C++. Most platforms I know will just give you a reinterpretation of the two
char values as one short value, but the right tool to do this is a
reinterpret_cast.

Except in the case of reading from a char or unsigned char aliased to
something else (unfortuntaely, this is the opposite of what is happening
in this case).
 
D

Default User

Ron said:
Except in the case of reading from a char or unsigned char aliased to
something else (unfortuntaely, this is the opposite of what is
happening in this case).

Isn't there something about structs with the same initial sequence, you
can examine the common part? I'm pretty sure that's true in C, not sure
if it carried over to C++.

Obviously wouldn't apply here, regardless.




Brian Rodenborn
 
M

Mabden

Default User said:
Isn't there something about structs with the same initial sequence, you
can examine the common part? I'm pretty sure that's true in C, not sure
if it carried over to C++.

Sure, in C you can pass a pointer and cast it anything you want, but
it's pretty dangerous. But I once worked on a system that passed around
several dozen structures that were identical in the first 8 or 10
fields, and then had different members below that. This was before C++
(there I mentioned it, so I'm ON TOPIC). The functions kind of
daisy-chained their way down to the final function that separated them
out to their final destination, if necessary. Sometimes a basic
operation was performed on any structure, sometimes only a certain
class, based on a field. I wish I had the code to cite a better example.

Of course, this was a hard environment to debug since any number of
different structs may pass through a function and you lose any debug
info about the struct that was passed in.

The main lesson I learned from that system was: Don't Do That!
 

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

Forum statistics

Threads
474,178
Messages
2,570,955
Members
47,509
Latest member
Jack116

Latest Threads

Top