From: anonymusius on
I'm sorry if this is an noob qustion but what is the exact point of
having two variable's on the same memory adress in an union? Wouldn't
it be better/usefuller to just declare one variable?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: simon.rutishauser on
Hi,

On Sat, Jul 15, 2006 at 06:37:20AM -0400, anonymusius wrote:
> I'm sorry if this is an noob qustion but what is the exact point of
> having two variable's on the same memory adress in an union?
> Wouldn't it be better/usefuller to just declare one variable?

The point might be that you are sure that you will only use one of these
variables (usually having different types) at a time. So it might be a
way to save memory for instance when passing data to a function where
the type of data is not always the same.

Actually that's the only use of unions I have seen in practice so far
(and it was *not* me who wrote the code).

The problem ist that it might happen that even though you thought you
wouldn't use both variables that are in the union at the same time,
someone somewhere does. I think this has the potential for lots of
bugs that are not easy to spot.

Actually I don't think there is much use for unions in C++ nowadays.
Maybe there was some in C - but I think you'd much better use derived
classes or something else rather than unions.

Greetings
Simon

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Chris Hills on
In article <1152894252.318739.207210(a)m79g2000cwm.googlegroups.com>,
anonymusius <annoniemo(a)gmail.com> writes
>I'm sorry if this is an noob qustion but what is the exact point of
>having two variable's on the same memory adress in an union? Wouldn't
>it be better/usefuller to just declare one variable?

It is a way of converting things. You can put 2 chars (or 8 bit ints) in
and get an int (16 bit ) out etc.



--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ chris(a)phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Thomas Richter on
anonymusius wrote:

> what is the exact point of
> having two variable's on the same memory adress in an union? Wouldn't
> it be better/usefuller to just declare one variable?

Unions are more or less a legacy feature of C that still made it into
C++, and that is not very often used today. In very memory-critical
situations you'd like to be able to re-use the same memory location
twice (or even more often) depending on some type descriptor, e.g.

union {
int i_number;
float f_number;
};

would be able to store either an integer or a floating point variable
describing one quantity in the same memory location(s). Note the
"either-or", as this union cannot hold an int and a float
simultaneously. As what it holds is up to the interpretation of the
program, and this information must be stored elsewhere.

Or, another application is very specialized hardware that maps several
functions onto the same memory location. Unions are also sometimes
(mis?-) used to re-interpret the bit-pattern of a specific internal
representation. The above example *could* be used (on some
architectures, on some compilers) to re-interpret the bit-pattern
describing a floating point variable as an integer.

So long,
Thomas

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: kulkarnivarad on
anonymusius wrote:

> I'm sorry if this is an noob qustion but what is the exact point of
> having two variable's on the same memory adress in an union? Wouldn't
> it be better/usefuller to just declare one variable?
>

If you know about the cpu registers, union is the useful way of
addressing them. Each register of 16-bit in length (AX,BX,CX etc) can
be modified using two 8-bit variables(for AH, AL etc), which is really
useful in calling of interrupts, when you want to maintain clarity as
well.

This is the structure defined in dos.h :

struct BYTEREGS {
unsigned char al, ah, bl, bh;
unsigned char cl, ch, dl, dh;
};

struct WORDREGS {
unsigned int ax, bx, cx, dx;
unsigned int si, di, cflag, flags;
};

union REGS {
BYTEREGS h;
WORDREGS x;
};

REGS regs;

So you can access entire AX register as regs.x.ax or you can access
each high or low byte register AH or AL as regs.h.ah or regs.h.al.

e.g. To display a string on text-mode screen using BIOS interrupt 10h
using function number 0Ah:

#include <dos.h>

// set the function number in AH, character to display in AL,
// the screen page number in BH and the number of times the character
// gets repeated in CX

void main (){
REGS in,out;

in.h.ah=0x0a; // the interrupt function number
in.h.al='a'; // character to display
in.h.bh=0; // page number
in.x.cx=1; // repeatation

int86(0x10,&in,&out); // call interrupt 10h
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 |  Next  |  Last
Pages: 1 2 3
Prev: std::ws
Next: (Re) Constructing an object