Association in c++ (union C++)

Unions in c ++, union c++, report, course workUnited Technologies union It has its origins in the '90s. Weak computer for our times (Now their computers and it can not be called), low memory (all measured kilobytes). Hard Drives for 40 megabyte was almost a miracle of technology, personifying ogromennye amounts of information, craftsmen that “bore” special software to storage 80 megabyte (so that was a little more). Each byte of memory was at a premium, I had to save on everything. That's an association variable was also intended (and quite successfully, as I will explain below) help the programmer to make optimal and economical program, “eat” small amounts of memory.

To understand what is the meaning of association need to remember how variables are stored. Various different types of variables or the same type of group (like int, long and short) despite the work with the same data type, (I mean the whole) occupy a different amount of memory in bytes. long in any case it takes a maximum number of bytes in the memory, at the same time in the memory for a variable of this type it is possible to write down the value int or short. Just get, that not all reserved bytes long-and will be in demand. If you put for example the number of 325 in long, two bytes are occupied (It depends on the bit processor), and the remaining bytes are filled with zeros.

It is in this sense appears union, For this statement tells the compiler: “I will reserve a place for the data type with a maximum query memory, and I'll sort itself, how and what value to put in them”.

An association – is a data format, which is similar structure. It (an association) capable of storing within one reserved memory area various types of data. But at any given time it is stored in association only one of these data types, and may use only the value of the element (component). Syntactically define the union as (very similar to the definition of the structure):

Access to the elements of the union is the same, as well as to the elements of structures: The name of association myUnion , point . and the name of the element name1 .

data, which store elements structure (for example short, int, long) we can access at any time (at least one, though all at once). in merger data can be stored either short, or int, or long. For example:

Run the program:

Unions in c ++, union c++, report, course work

As you can see, after, we recorded the value in element name3 type long int , normally no longer possible to access the element name1 . This is because, in their shared memory already recorded value long int, and the variable type short int unable to work with such a volume of data. Schematically it can be represented as:

Unions in c ++, union c++, report, course work

Therefore,, to once again work with the type of data short int you must assign the item again name1 new meaning. So it turns out – memory of one and the same, and the variables in it are placed different. To what call – and such requests from this memory value.

The elements are arranged in association with the memory from one location (as it were superimposed on each other), in contrast to the structural elements (they are stored in memory consecutively, one after another).

Application of association due to the need to save memory, when you need to store and use data types, but to access them, you can not simultaneously.

If we describe a set of variables are not bringing them in union, then placing them would require 2 + 4 + 4 bytes = 10 bytes. That savings. And the union takes 4 bytes. As many as 6 byte saved, filling three variables in one memory segment.

By the way it should be noted, that this technology has managed to get a good continuation, and in the modern languages ​​used everywhere where it is necessary and do not need. SiŞarp, PHP, Delphi, modern heirs C plus plus – they use all such associations. Only in the modern world they are called variant. The variable variant You can easily shove up 10 bytes (10 this is the maximum for 32-bit systems under real number). Accordingly, these 10 byte and the entire family of integer and real, and pointers, objects, and string (more precisely handles strings or pointers to them) fit.

Today, the memory savings are not so relevant, how 15-20 years ago. Of course, nobody forbids to use union, but why? If there are more convenient for today means working with memory programmer.

It should also be noted, that to perform a program memory loaded with excess (i.e.. to program the amount of memory allocated, very often more than necessary). This can be observed, writing a program with one variable. It would seem – variable 4 bytes, but by opening the Task Manager in Windows software “somehow” won 10 kilobyte. This is how modern operating system, allocating memory with a vengeance. In the 90, Alas, a luxury and could not be.

In the next lesson we will look at bit fields in C ++. Take the time to watch a video on the theme of Association (union) in С++ :

Newsletter of programming:

Association in c++ (union C++)
3.5 (70%) 4 votes

Leave a Reply

Your email address will not be published. Required fields are marked *