• C++ - Unions?
    3 replies, posted
Hey there. Now, I've been learning C++ for a few months, however there was something I never have picked up, what are Unions? According to [url]http://en.cppreference.com/w/cpp/language/union[/url], "A union is a special class type that can hold only one of its non-static data members at a time," What does this mean? As in, you can only access one value from it if you're accessing a value in a function, or something similar to that? I don't quite undertsand. Also, when should I prefer unions over structs/classes? Thanks in advance for the help.
Unions are really nothing like classes or structs. A unions members all inhabit the same region of memory, which is what their use is. For example, you could have a structure that could hold either a float or an integer, as well as a boolean to tell you which it is: [cpp] struct anynumber { bool isfloat; union { float flt; int itgr; } } [/cpp] If you used a struct (or class) in place of the union in the above example, it'd have a total size of 8 bytes*, with the boolean taking up an extra byte**. But since we're using a union, both the integer and the float are occupy the same four bytes, bringing the total to 4 (or 5 with boolean). The use of this is keeping one of more variables in the same space, without having to specifically define, say a char[4] array and then cast the pointer to the different types it could be. It's important to note that writing to one of the members of a union, and then reading from another is classified as undefined behaviour, although quite a few libraries today rely on it being perfectly fine on all the common compilers. * Depends a bit on platform I believe, but assuming both floats and ints are 4 bytes, and struct packing size is no greater than 4 ** Unless specifically told, the compiler will most likely not pack the structure "anynumber" tight enough to yield a 9-byte struct and will most likely pad it to 12 bytes for greater efficiency.
[QUOTE=Dr Magnusson;49611987]Unions are really nothing like classes or structs. A unions members all inhabit the same region of memory, which is what their use is. For example, you could have a structure that could hold either a float or an integer, as well as a boolean to tell you which it is: [cpp] struct anynumber { bool isfloat; union { float flt; int itgr; } } [/cpp] If you used a struct (or class) in place of the union in the above example, it'd have a total size of 8 bytes*, with the boolean taking up an extra byte**. But since we're using a union, both the integer and the float are occupy the same four bytes, bringing the total to 4 (or 5 with boolean). The use of this is keeping one of more variables in the same space, without having to specifically define, say a char[4] array and then cast the pointer to the different types it could be. It's important to note that writing to one of the members of a union, and then reading from another is classified as undefined behaviour, although quite a few libraries today rely on it being perfectly fine on all the common compilers. * Depends a bit on platform I believe, but assuming both floats and ints are 4 bytes, and struct packing size is no greater than 4 ** Unless specifically told, the compiler will most likely not pack the structure "anynumber" tight enough to yield a 9-byte struct and will most likely pad it to 12 bytes for greater efficiency.[/QUOTE] Thanks! So, they can be used for kind of an 'either' thing to save memory? As in, you could make it so that either a boolean [i]or[/i] float is defined instead of both, just to clarify?
[QUOTE=MaximLaHaxim;49612261]Thanks! So, they can be used for kind of an 'either' thing to save memory? As in, you could make it so that either a boolean [i]or[/i] float is defined instead of both, just to clarify?[/QUOTE] Exactly, yeah. Just keep in mind that the unions size will always be the size of the biggest member variable (or bigger), so a union of a float and a boolean would still be 4 or more bytes.
Sorry, you need to Log In to post a reply to this thread.