08_46 – Data structures UnionOn February 16, 2020 by Raul Dinwiddie
Hello, In this video, I’m going to talk about a new keyword, called “union”. unions look a lot like structures, except that structures use to add data, one after the other. When we did a points structure, earlier, as I showed you in another video, where I had 2 ints next to each other, This time, unions will allow us to access the memory areas, but in several different ways. Again, the best way for me to explain this is to show you. So, I’ve got a structure s_test, which has an int “i”, a char “c”, a float “f”, and a char “tab”, Let’s create the union. The same way we’d create a structure. Union Let’s call our union, u_test. Semi-colon at the end. I’m going to do the same thing inside. You’ll see, it’ll be useful. As I’ve told you, the difference between unions and structures, is that structures add elements one after another, and the union will allow us to access the same memory area, in many different ways. For example, if I use the keyword sizeof, which will give us the size of an element, so for example the size of my structure s_test. Here we go. Let’s display it. Here, we notice my structure is 16 byte. Let’s check. An int=4 bytes, a char is one byte, so that makes it 5, a float is 4 bytes, so we have 9. and a char tab, so 4 more bytes. That makes 13 bytes. Not 16… But why? It’s quite simple: My compiler does some padding between my char and the float, because in order to access memory faster, basically the compiler will position the elements, at addresses that are multiples of 4, as the processor is “32 bits”, it’ll create access on 4, if we had a 64 bits processor, it’d have created access on 8… and therefore padding on 8. I’m gonna prove it to you. If I set a char xx here, recompile, and relaunch. Still 16. Now, if I moved this char, and then added it bellow the float, well with this, we’d get 20. Why? Simply because there’s been an extra padding of 1 So, in the end, my char, meaning that with 3 bytes, there’s a padding of 4. So the size of my structure, is the sum of all the elements to which we add the padding. Now let’s use Union, and you’ll see how it changes every thing. I’m gonna use my sizeof on my union. here, it indicates a size of 4. Actually it takes the size of the biggest element. An int is 4, a chat is 1, a float is 4, and a char tab is 4. If I replaced it with tab, just for fun, we’d be at 12. Once again: padding (google it) I’ll put it back to 4. Let me show you how it works. Let’s create a new object of type “union” so “union u_test”, which we’ll call “a” and indicate that if “a.i=42” Let’s use a bigger number than 42… how about 424242, It’ll be clearer. My idea… is to first display “a.i” to show you its value is indeed 424242. Here we go. Now, let’s try accessing my int, going through my tab, and using each char from my char tab. as a piece of my int, (remember: 1 int=4 bytes) Normally, if I display: char tab, tab, tab, tab I should access different parts of my int. Here we go. Let’s start over. I’m going to add something to separate them. To make it clearer.