I'm working on the so called Hotspot open source project, and looking at the implementation I found a nasty nested union in struct looking like that:
typedef struct RC_model_t_st
{
union
{
struct block_model_t_st *block;
struct grid_model_t_st *grid;
};
/* block model or grid model */
int type;
thermal_config_t *config;
}RC_model_t;
As far as I'm aware in C/C++ that union is unaccesible. So how someone can make use of union declared in such manner and for what purpose?
Thanks!
first of all i want to say that A union, is a collection of variables of different types, just like a structure. However, with unions, you can only store information in one field at any one time.
The unions are basically used for memory saving & it's size is equal to the largest member of the union.
And for accessing the data fields of a union, use the dot operator(.) just as you would for a structure and explained by @Atmocreations. When a value is assigned to one member, the other member(s) get whipped out since they share the same memory.
as an example where the unions may be useful is
.... The union above could be used to either store the current time (in seconds) to hold time accurate to a second. Or it could be used to hold time accurate to a millisecond. Presumably there are times when you would want one or the other, but not both. This declaration should look familiar. It is the same as a struct definition, but with the keyword union instead of struct.
for more info http://msdn.microsoft.com/en-us/library/5dxy4b7b(v=vs.80).aspx
Names declared in an anonymous union are used directly, like nonmember variables. A good reason to do this is to save memory.
To elaborate on the answer provided by Angew quoting the standard concerning anonymous unions and structs, I thought to provide an sample of C source code with the output generated by that sample showing how values are allocated within a
struct
and aunion
composed ofstruct
andunion
components.The standard quoted by Angew is:
The source code of a
struct
composed of named and anonymous structs and unions looks like the following. This is using Visual Studio 2005 and the#pragma (pack, 1)
is used to align everything on achar
boundary in order for there to be no memory holes. There is also a simple C Preprocessor macro defined to make the output more legible and easier to code.The output generated by this function looks like:
The output shows the overlap between the various components of the main
struct
,Things
caused by the use of unions. You can also see how the components of the anonymousstruct
andunion
are referenced versus those components of the namedstruct
andunion
.Also just for fun I tried adding an array definition of
const UCHAR myArray[];
after the anonymousunion
containingconst UCHAR myArray[];
to see what would happen. The compiler complained with an error oferror C2020: 'myArray' : 'struct' member redefinition
. The addition is commented out in thestruct
definition ofThings
above. However since the second use ofconst UCHAR myArray[];
is in a namedunion
the compile works because second use is accessed by specifying the name of the union.Without being sure and without having tried:
The union itself is not accessible, but it's members are.
Therefore you should be able to do refer to
obj.block
andobj.grid
This is an anonymous union. In C++, as per [class.union], paragraph 5:
This means you can access its members as if they were members of
RC_model_t_st
.This code here (https://gist.github.com/klange/4042963) shows how to access anonymous unions inside struct. You just access the members of nested union as if they are members of the struct.