tags:

views:

99

answers:

6

In union we know the memory used is maximum of the one occupied by its element but how on the same memory space different elements of the union can be stored ,are they not overwritten on the same memory space which cause to lose the prior element present there,i actually want to know the exact working of union while storing the different element..........

+1  A: 

Yes, a union does overlap members, so changing one can have the side-effect of changing another. That's kind of the point.

Steven Sudit
A: 

You seem to be laboring under a misapprehension.

Assigning a new value of a different type does overwrite the existing data.

dmckee
A: 

It's nothing more than a way to view or interpret the memory underlying the union structure. i.e. are these 4 bytes an integer, or a pointer, or contribute to a double etc ? (for example). Consequently, writing to your int will affect your pointer value, or your double.

And yes - writing a value in will overwrite the previous value - just like a normal struct (or anything else).

Brian Agnew
I think the key he was missing is that, since values overlap, overwriting one also affects others.
Steven Sudit
Edited to reflect that. Thanks.
Brian Agnew
+1  A: 

I think you might be confused about what a union is.

These MSDN pages here (c) and here (C++) go into more detail but basically a union is a mechanism where the same memory address can be referenced in different ways depending on the circumstances:

union sign   /* A definition and a declaration */
{
    int svar;
    unsigned uvar;
} number;

This example defines a union variable of type sign and declares a variable named number that has two members: svar, a signed integer, and uvar, an unsigned integer. This declaration enables the current value of number to be stored as either a signed value or an unsigned value. The tag that is associated with this union type is sign.

ChrisF
+1 for being comprehensive and clear
Steven Sudit
+3  A: 

You are right. Memory is overwritten since all elements share the same space. Take a look at this:

union foo{
    int i;
    char[4] carr;
};

Assuming integer is 4 bytes and char is 1 byte, if you modify the integer, all the four elements of the char array will be modified.

I would strongly suggest writing your own small code (with appropriate printfs) to understand the working of union. You will learn faster that way.

Ashwin
A: 

They are overwritten, i.e. every time you assign one union member you change the other. That is actually the intent of the union in C - to be able to have different typed views on the same bits in memory. Classic example is checking the endiannes of the platform at runtime (silly but illustrative):

union duo
{
    uint16_t short_;
    uint8_t  bytes[2];

} the_duo;

/* will assign either bytes[0] or bytes[1]
 * depending on the architecture */
the_duo.short_ = 1;

if ( the_duo.bytes[0] )
    printf( "little endian\n" );
else if ( the_duo.bytes[1] )
    printf( "big endian\n" );
else
    printf( "where am i?\n" );

Nikolai N Fetissov