Friday, May 27, 2022

#50 Embedded C – Difference between Struct & Union

Hello and welcome in my previous videos i have talked about struct or structure and union and if you have watched my previous videos you will see that struct and union .

They almost look alike so in this video i like to explore the difference between struct and union so difference between struct and union now the main difference between struct and union is how memory is allocated to .

Each member of struct on union with struct the compiler would allocate memory for each data member within the struct but for unions or union the compiler will allocate a block of memory equal to the largest .

Data member in the union so let's take a look at struct so here i'm repeating from my previous video an example so i have a struct with a structure data type here called access data .

Which consists of two members or elements so the first member is pin of the type int and the second member is called zone of the type unsigned char now pin and zone each .

Each of these member will have its own memory location now in their birth case pin because it's of the type in so it's 16 bits so it needs requires sorry two bytes of uh memory stock storage and zone which is an unsigned char will .

Need eight bits so it's a byte so you add them together uh pin two bytes zone one byte so it's two plus one and therefore this particular uh in this particular example it will require uh two plus one three bytes .

Uh to store the structure access data now for union the total uh memory space allocated is equal to the member with with the largest size so if we look at this again so we have pin .

Which is 16 bits we have two bytes and zone which is eight bits because of its unsigned char that's one byte so the total memory uh allocated to this union type uh would be two bytes .

So all members uh that is in this example pin and zone will share the same memory space okay so we are back in the kailadi ide so i'm going to take a look at the struct data type again i've used this same code in my previous .

Video on when i was talking about the struct or structure so um let me zoom in let me just enlarge this uh i'm i've let me just compile it .

I'll build it yeah so i have zero errors zero warnings um let's go straight to debug and before i start single stepping let me just point out a couple of things here that is to look at how uh memory location is being allocated to .

The members of the structure all right so um let's have a look at it let's look at this uh disassembly part of the tile so for pin for or pin member so pin .

It's been allocated two locations that is zero eight and zero nine y two locations um this reminder pin is of the type in and in the type in is 16 bits which means two bytes so we need two bytes of memory location so the compiler has allocated uh .

Location zero eight hexa and location zero nine hexa for zone now zone is unsigned char which means eight bits one byte so it's been allocated uh zero x zero a which is zero a hex so uh not forgetting that zero a comes after .

Zero nine so these are the uh location allocated to pin and zone respectively okay so just keep in mind 0 8 0 9 and 0 a so let me just get out of this mode .

All right so we go back into single step mode now for this demonstration i am just going to focus on uh just a minute let me zoom zoom in here and i'm going to just focusing on zero eight location zero eight and location zero nine and location zero a because these .

Are the locations allocated to uh the member's pin and zone respectively so let me just zoom out and out of the editing mode right so i'll single step and let's keep an eye on this data .

Memory locations here that i mentioned so the first thing is to initialize the timer which we're not going to use in this one because we are just interested in how memory location is allocated to the members and how they have been accessed so now we're going to access the members first of all uh pin .

So we're going to make use of the code sorry the dot operator so it's dot code dot pin assigned with decimal value nine three five five uh so watch this memory window here location zero and 0 9 and we have 2 4 .

8 b now 2 4 8 b is the hex equivalent of 9 3 5 5 which is decimal and you can see as i hover over the uh the 9355 you can see the hex equivalent which is this identical to the one here 2 4 8 b it should be because they are exactly the same now .

Zone zone uh it's been allocated here code dot zone has been assigned uppercase a so watch the location 0 a because that's where that's what the compiler has allocated zone .

And we have 4 1 4 1 is the hex value for uppercase a all right so that's how the compiler has allocate allocated the memory to pin and zone and that's how they have been accessed so each member has its own .

Memory locations okay so let's take a look at the union data type so as with the previous example on struct data type so i'm going to do the same we're going to focus on in this demonstration on how the compiler allocates uh memory locations to .

The members of the union so in this case the members of the union is pin of the type int so requires uh two bytes of location and zone is an unsigned char so uh it's only eight bits so it's one byte .

Of uh memory location that it requires so and then we'll see how the members have been asked the union members have been accessed and how a memory is being utilized all right so let's .

Uh i've already compiled the code so let's go straight to debug okay so let's focus on the disassembly for a while let's take a sorry let's take a look at this right so the compiler has .

Allocated 0 8 0 9 location 0 8 and 0 9 hexa to the to pin which is exactly the same as in the previous example which dropped now for zone uh which is unsigned char it bits one byte .

Uh it's allocated watch this zero eight hexa so if you were to uh look at the first byte of the memo location of pin is 0 8 and zone is also allocated 0 8 hexa why is that that's because all mem all other members .

In the union share the same memory size and it's uh we're not surprised that it's zero eight for uh the lower bank of uh pin is zero eight and the location for a capital uh uppercase a .

Zone sorry rather zone is also allocated zero eight all right so enough of that let's come out of this and so let's go to the debug and let's step through so we initialize the timer .

And notice we will take note of the memory location here so let's zoom in quickly so is this three locations that we want to take note uh sorry these two location we want to .

Take note 0 8 and 0 9. all right so let's zoom out again all right so we're here so now 9 3 5 5 which is 248 b 248 b in hexa will be .

Assigned to code dot pinned so watch out for the memory address here yes we have let's do a quick zoom in so uh at 0 8 and 0 9 we have 2 4 8 b now let's move on to the next line now watch 0 8 and 0 9 here focusing on .

Uh zero eight location zero eight right now it has two four which is the uh upper bike of the pin so it's 2 4 watch .

What happens when i step through the next line which is uh code dot zone assign uppercase a uppercase a if okay so watch zero eight location zero eight now the location zero contains four one four one as you remember from .

The previous demonstration with struck four one is actually the hex code for uppercase a so now this result shouldn't uh surprise us let me just zoom in to the memory remember uh the memory space allocated is equal .

To the member with the largest memory size which is two bytes that because pin is two bytes needs two bytes of location so and also remember all members share the same memory space all right so that's why uh .

Four one is in store and zero eight now which means there's something uh we have to take note when you when using union that the uh uh a member could override or corrupt the contents of the of another member .

In the same union all right so we just have to bear that okay so let's do a quick review or a recap of uh what just happened so struck with the stroke uh type the members each member within the stroke is allocated its own memory so pinned .

Of the type ins that's 16 bit two bytes of location is needed so the compiler will allocate two bytes to pin and zone which is a unsigned char 8 bits one byte so the compiler will allocate .

One byte to zone so each member has its own memory allocation where where else in union type the member pin is the largest member in the union meaning this is remembering this requires two bytes .

Of mem location and this uh zone here requires only one byte so the uh compiler allocate a block memory equal to the largest member in the union in this case is pin so two bytes is allocated so in our example that we just seen the .

For struct type the compiler has allocated uh zero eight hexa and zero nine hexa to pin for pin and zone is allocated uh the memory location 0a so struck type would have .

Memory location 0 8 0 9 0 a for his members and in the union pin is the largest member in the union it's two bytes so the compiler allocates uh zero location zero eight hexa and location zero nine hexa to the whole union type .

And so pin and zone will share the same memory locations all right so let's go to struck in in the stroke demonstration to access the member pin i use assign .

93555 decimal in hex that is equals to 248b so 2 4 is allocated to it will be assigned sorry to 0 8 location 0 8 and 8 b will be assigned to location location09 .

now when it comes to accessing zone with code dot zone with the uppercase a 4 1 that's the hex equivalent of for the uppercase a that would be allocated to location 0 a now let's take a look at union .

Now when i execute code dot pin assign nine three five five to access the uh member pin remember uh two nine nine three five five is the hex equivalence two for eight b as previously so 2 4 will be assigned to location 0 8 and 8 b assigned to location 0 9 .

But watch what happened when it come to code dot zone assigned with uppercase a that is uh four one hexa four one now replace two four because four one was is also allocated the location zero eight hexa remember each member .

Uh the members of the union share the same location so basically four one has overwritten uh two four so as we observe the value of pin is overwritten by the most recently stored value zone .

And this is simply because all members share the same memory space okay so that's it for this particular video i hope you find you find the information useful and and as usual i have included the code that i've used in the demonstration .

Given in the link in the description so thank you for watching see you in the next video bye


Most Popular