Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,630 --> 00:00:07,060
All right, so now let's talk about how this union variable basically looks like behind the scenes.
2
00:00:07,950 --> 00:00:14,700
So, first of all, simply saying when you created this variable, these my variable one of type union
3
00:00:14,700 --> 00:00:23,310
info, then this union definition basically seems like to have a few fields.
4
00:00:23,310 --> 00:00:23,730
Right.
5
00:00:23,730 --> 00:00:29,640
There is the first name in the age, but this does not mean you created a structure.
6
00:00:29,650 --> 00:00:34,090
So if it was like I don't know if it was like, let's say Trachte.
7
00:00:34,150 --> 00:00:36,480
OK, so I don't know, tracting info.
8
00:00:37,530 --> 00:00:40,950
And in this case there were like also two fields.
9
00:00:41,000 --> 00:00:44,190
OK, so field one in field two.
10
00:00:45,450 --> 00:00:50,020
And basically then when you would have created the struct, it will look like this.
11
00:00:50,040 --> 00:00:53,730
So there was like a separate place for each field.
12
00:00:53,740 --> 00:00:59,280
So this was a place in memory for field one and a place in memory for field two.
13
00:00:59,760 --> 00:01:03,240
But that's basically not what happens for unions.
14
00:01:03,240 --> 00:01:07,410
For unions, there will be like one place, OK, in memory.
15
00:01:07,560 --> 00:01:15,150
And this place is going to be of the largest of the size of the largest element in the union structure,
16
00:01:15,150 --> 00:01:17,060
in the union definition.
17
00:01:17,340 --> 00:01:27,120
In this case, we know that these int age are acquirer's like four bytes of memory and these characters
18
00:01:27,120 --> 00:01:28,880
are Raees of twenty bytes.
19
00:01:29,490 --> 00:01:36,790
So what we can say that the the memory that will be spent for these variables.
20
00:01:36,790 --> 00:01:46,440
So my variable one, it will not be like 20 bytes plus four bytes, which was like something for the
21
00:01:46,440 --> 00:01:53,490
structure of the world, like 20 bytes for the first name array of characters and four bytes for the
22
00:01:53,490 --> 00:01:53,820
age.
23
00:01:54,090 --> 00:02:00,120
But rather since that's a union, we are going to have only for the largest field.
24
00:02:00,150 --> 00:02:04,350
In this case, it's just 20 bytes of memory.
25
00:02:04,460 --> 00:02:14,100
OK, and from this place, from this memory region, we are going basically to be able to store information
26
00:02:14,100 --> 00:02:18,930
regarding first name or the age so either of them can be stored here.
27
00:02:18,930 --> 00:02:25,830
But you see, whenever you're going to write some information, let's say for the first name inside
28
00:02:25,830 --> 00:02:32,040
of this region in memory, then the value for the age may also get changed.
29
00:02:32,610 --> 00:02:38,200
And let me show you, I think that better example to demonstrate exactly what I mean.
30
00:02:39,330 --> 00:02:41,850
So let's go and do something like this.
31
00:02:42,750 --> 00:02:48,660
Well, let's say my variable, my variable, one dot age equals two thirty.
32
00:02:49,050 --> 00:02:51,230
Now let's use, I don't know, some print line.
33
00:02:51,240 --> 00:02:58,440
OK, so let's use let's use print F to F and print my variable.
34
00:02:58,440 --> 00:03:05,290
One dot age equals two percentage D basically see what it will print us.
35
00:03:05,310 --> 00:03:11,910
So my variable one dark age and let's build and run this program and see what happens.
36
00:03:12,980 --> 00:03:17,400
So basically, there you go, you can see what is printed on the screen.
37
00:03:17,420 --> 00:03:23,440
Let me just remove all of these drawings so you can see my variable one Dorridge equals to 30.
38
00:03:23,570 --> 00:03:26,300
So everything as expected so far.
39
00:03:26,400 --> 00:03:35,270
OK, but if we are going to do something like this, let's see my variable one, my variable one dot
40
00:03:35,900 --> 00:03:38,080
first name for his name.
41
00:03:38,090 --> 00:03:40,740
Let's say it will be equal to Hello String.
42
00:03:41,190 --> 00:03:47,350
OK, so there will be like information regarding these first name array of characters.
43
00:03:47,360 --> 00:03:52,660
So we are going to put there, I don't know, something like these little nice string.
44
00:03:53,480 --> 00:03:56,570
And I'll let's try to build and run it and see what happens.
45
00:03:56,600 --> 00:03:58,700
OK, so basically.
46
00:03:58,730 --> 00:04:00,950
Oh of course.
47
00:04:00,950 --> 00:04:01,400
Of course.
48
00:04:01,400 --> 00:04:04,010
Of course we cannot do it this way.
49
00:04:04,010 --> 00:04:06,350
So let's just fix it up quickly.
50
00:04:07,190 --> 00:04:11,660
Let's just include the string that h liberatory.
51
00:04:11,660 --> 00:04:15,500
So string dot age and basically use here.
52
00:04:16,400 --> 00:04:21,300
Let's use here is the CPI if you want.
53
00:04:21,310 --> 00:04:21,620
Yeah.
54
00:04:21,620 --> 00:04:26,540
If I'm not mistaken it's the RCP y and passing here two values.
55
00:04:26,780 --> 00:04:33,980
So you can see the, the signature and the structure basically of these SDR supervisor copying one thing
56
00:04:33,980 --> 00:04:38,500
and from copying these string into this region in the memory.
57
00:04:38,510 --> 00:04:43,370
So let's try to build and run it once again, a semicolon at the end.
58
00:04:44,420 --> 00:04:49,160
So building Iranians, OK, so now everything seems to be working correctly.
59
00:04:49,850 --> 00:04:57,410
But now what I want us to do is simply let's try to print this string to the screen.
60
00:04:57,470 --> 00:04:58,510
OK, so let's see this.
61
00:04:58,520 --> 00:05:01,910
I don't know, put us and put this string to the screen.
62
00:05:01,910 --> 00:05:14,030
So let's say let's better say print out my variable dot first name equals two percentage s and basically
63
00:05:14,030 --> 00:05:20,880
simply saying, oh, my variable dot first thing first.
64
00:05:21,410 --> 00:05:25,250
OK, what do you expect to see now on the screen?
65
00:05:25,280 --> 00:05:26,600
Do you expect to see hello.
66
00:05:26,600 --> 00:05:28,190
Do you expect to see something else.
67
00:05:29,120 --> 00:05:31,160
So let's build and run it and see what happens.
68
00:05:32,210 --> 00:05:39,290
So it seems like everything is working correctly and it's very similar to how this products work in
69
00:05:39,290 --> 00:05:40,670
C programming language.
70
00:05:40,820 --> 00:05:45,500
So there is like my variable one age equal to 30, my variable.
71
00:05:45,500 --> 00:05:47,090
That first name equals to hello.
72
00:05:47,450 --> 00:05:55,430
So it seems OK, it seems like are from first glance it seems like there are basically two fields,
73
00:05:55,430 --> 00:05:59,650
one for the age and one for the first name.
74
00:06:00,440 --> 00:06:07,070
And basically here we have like a value of thirty and here we have like a string of hello.
75
00:06:08,520 --> 00:06:14,640
So everything seems to be fine, but if that's the case, if that would be the structure, then it would
76
00:06:14,640 --> 00:06:21,570
make sense that if I'm trying if I would try to print this value once again, if I would try to print
77
00:06:21,570 --> 00:06:26,520
Vade once again, then basically the value of 30 will also be printed.
78
00:06:26,520 --> 00:06:26,970
Right.
79
00:06:27,870 --> 00:06:31,020
So let's build it and run it and see what happens.
80
00:06:31,560 --> 00:06:32,850
So there you go.
81
00:06:33,000 --> 00:06:36,990
That's the new execution of this program.
82
00:06:37,320 --> 00:06:46,860
So beforehand, my variable, one age was equal to 30 and then reprinted below because we use the recipe.
83
00:06:46,910 --> 00:06:52,440
Why was simply assigned the string hello to the my variable one doctor's name.
84
00:06:53,250 --> 00:06:58,020
And then we printed it out to the screen and then we did not change it.
85
00:06:58,020 --> 00:07:03,060
Any step in this program, my variable one that age once again.
86
00:07:03,570 --> 00:07:08,800
But the value printed here does not make any any sense at all.
87
00:07:09,300 --> 00:07:11,190
So why was it changed?
88
00:07:11,220 --> 00:07:17,730
Why was this my variable at H equal to 30 now changed to this strange value.
89
00:07:18,390 --> 00:07:20,190
And the answer is very simple guys.
90
00:07:20,370 --> 00:07:27,920
That's because we used unions and not a structure where in the structure that there was a specific separated
91
00:07:27,930 --> 00:07:39,240
block for of memory for each of these fields and for unions, we simply have one region in memory that
92
00:07:39,270 --> 00:07:43,130
this region will store information for either of them.
93
00:07:43,200 --> 00:07:45,690
OK, so there is this age.
94
00:07:45,990 --> 00:07:51,000
First of all, you put here 30 and then you said there was also a first name.
95
00:07:51,450 --> 00:07:53,680
And if I want to change its value.
96
00:07:53,700 --> 00:08:00,300
OK, so I simply remove and remove it and overrated these value of thirty.
97
00:08:00,300 --> 00:08:02,370
And I put it here like the string.
98
00:08:02,370 --> 00:08:02,940
Hello.
99
00:08:03,360 --> 00:08:13,440
OK, so then again, when you tried to like to access using this field of age in this union info variable
100
00:08:13,440 --> 00:08:18,570
that you created my variable one, then it's going to try to see the information.
101
00:08:18,750 --> 00:08:23,430
But the information regarding these 30 does not longer exist there.
102
00:08:23,610 --> 00:08:32,430
So it's like use this percentage D and kind of grabs some information from this L and so on.
103
00:08:32,580 --> 00:08:39,690
OK, so that's basically what happens behind the scenes when you are using union variables.
104
00:08:40,590 --> 00:08:41,240
All right.
105
00:08:41,250 --> 00:08:42,680
So I hope that's clear.
106
00:08:42,930 --> 00:08:51,030
And now just to emphasize and show you the differences, OK, show you the differences between struct
107
00:08:51,030 --> 00:08:51,960
and unions.
108
00:08:51,990 --> 00:09:00,360
OK, so let's first of all, see just are these Samori, that first thing a struct is just a block of
109
00:09:00,360 --> 00:09:11,970
memory, which stores alike like elements and fields that do not overlap while the union is also a block
110
00:09:11,970 --> 00:09:12,750
of memory.
111
00:09:12,900 --> 00:09:18,160
OK, but it's stores, these several fields on just one place.
112
00:09:18,180 --> 00:09:22,830
OK, so they store it like taking the largest of these fields.
113
00:09:22,830 --> 00:09:29,040
And that's the only memory that is being allocated for these union variable.
114
00:09:29,730 --> 00:09:39,390
And that's why you can only store one of the data fields at any point in time when you were using Unions'.
115
00:09:40,350 --> 00:09:42,600
So let's see additional example.
116
00:09:42,750 --> 00:09:46,880
Maybe it will be better to demonstrate how it looks like.
117
00:09:48,030 --> 00:09:49,530
So what's that?
118
00:09:49,570 --> 00:09:52,230
OK, so let's create struct.
119
00:09:52,740 --> 00:09:58,110
Let's create, let's create point struct.
120
00:09:58,350 --> 00:10:04,710
OK, so point struct in this structure is going to have two fields and X and Y.
121
00:10:05,020 --> 00:10:14,610
OK, and also we are going to create a union point union in this union is also going to have like two
122
00:10:14,610 --> 00:10:18,330
fields named X and Y.
123
00:10:18,930 --> 00:10:27,180
OK, and now the main function, let us let us create create two things.
124
00:10:27,180 --> 00:10:29,370
Let us first of all create a structure.
125
00:10:29,430 --> 00:10:35,640
OK, so let's create struct Trachte points struct.
126
00:10:36,640 --> 00:10:38,940
Let's call it point one.
127
00:10:39,060 --> 00:10:40,800
OK, that's the variable name.
128
00:10:41,580 --> 00:10:48,510
And also let's create a union union struct union or not union.
129
00:10:48,520 --> 00:10:49,410
What was that point.
130
00:10:49,410 --> 00:10:52,590
Union point to union point.
131
00:10:53,580 --> 00:10:57,540
Where is it, union formed union.
132
00:10:57,720 --> 00:11:01,150
OK, so union point union, let's go and point to.
133
00:11:01,650 --> 00:11:06,200
OK, so two very Jarrell's we created point one and point two.
134
00:11:07,020 --> 00:11:12,530
OK, now let us say the following thing.
135
00:11:13,410 --> 00:11:26,190
Let us say that points from point one dot x will be equal to five and point one that Y will be equal
136
00:11:26,190 --> 00:11:27,000
to seven.
137
00:11:27,570 --> 00:11:29,940
OK, so no problem about it.
138
00:11:29,940 --> 00:11:42,540
We can simply print like let's say plaintiffs are Throgs Point equals to like let's say percentage and
139
00:11:42,990 --> 00:11:43,890
percentage B..
140
00:11:44,310 --> 00:11:49,790
So here will be the X value in here will be the volume value.
141
00:11:50,610 --> 00:11:56,370
OK, so point one dot X and point one that Y.
142
00:11:57,040 --> 00:12:00,720
OK, so that's basically about FrontPoint.
143
00:12:01,800 --> 00:12:05,970
And if we will try to like to build and run it.
144
00:12:07,350 --> 00:12:13,110
Yeah, let's build and run it so you can see strongpoint equals two, five and seven, nothing special.
145
00:12:14,310 --> 00:12:24,690
And now what I want us to do is also to set up some information regarding regarding let's do it here
146
00:12:25,020 --> 00:12:26,060
regarding union.
147
00:12:26,520 --> 00:12:32,670
So point to point to that, X equals to let's say three.
148
00:12:33,540 --> 00:12:41,790
OK, and that was I'm going to print if I'm going to let's see if I'm going to print our union point
149
00:12:41,790 --> 00:12:46,080
equals to percentage and percentage point two and point two.
150
00:12:47,160 --> 00:12:53,670
OK, so I did not actually initialize any value for the Y field in this union.
151
00:12:53,670 --> 00:12:54,030
Right.
152
00:12:54,450 --> 00:13:00,730
Basically I've done it for the struct, but I haven't done it for the union.
153
00:13:01,170 --> 00:13:03,330
So if I'm going to build and run it.
154
00:13:04,340 --> 00:13:06,150
What do you expect will happen?
155
00:13:06,590 --> 00:13:14,720
So basically, if we will simply we're not using point one dot y equals to seven, then in this case
156
00:13:15,470 --> 00:13:21,200
for a struct, there will be like strange value, I don't know, 50 somehow got here, 50.
157
00:13:21,830 --> 00:13:24,820
I don't know where from this value even got here.
158
00:13:24,830 --> 00:13:33,320
So basically if we will build and run it, you will see that now struct point equals to five and seven
159
00:13:34,130 --> 00:13:37,670
and the union point equals two, three and three.
160
00:13:37,820 --> 00:13:45,560
So why do you need initialize in whity to assign a value of three also to the Y?
161
00:13:46,490 --> 00:13:57,690
So that's again the explanation for how you use basically the the difference between struct and unions.
162
00:13:57,950 --> 00:13:59,930
So here is a struct.
163
00:14:00,120 --> 00:14:05,040
OK, that's the struct for variable point one.
164
00:14:05,090 --> 00:14:06,680
OK, so that's point one.
165
00:14:08,120 --> 00:14:15,620
And it has two fields X which equals two five and Y which goes to seven.
166
00:14:16,190 --> 00:14:17,370
And from the other hand.
167
00:14:18,080 --> 00:14:26,240
OK, so first of all, here are four bytes of memory and also here are four bytes of memory, but four
168
00:14:26,240 --> 00:14:29,240
point two, which is of a union type.
169
00:14:29,880 --> 00:14:33,960
There is no special dedicated field for X and Y.
170
00:14:34,040 --> 00:14:36,740
OK, there is no special field.
171
00:14:37,040 --> 00:14:44,960
There is only four bytes of memory, OK, for bytes of memory since the largest element, the largest
172
00:14:44,960 --> 00:14:47,510
field here is a four bytes of memory.
173
00:14:47,900 --> 00:14:51,080
And inside of this area, we simply store a value.
174
00:14:51,410 --> 00:14:54,700
So when we said point to dot X equals the three.
175
00:14:54,740 --> 00:15:01,490
So you go to this region in memory and you set up here three and you can access these three from this
176
00:15:01,490 --> 00:15:03,140
X or from this Y.
177
00:15:04,070 --> 00:15:14,780
That means that if we are going to change to change the value of X or Y, let's say point to dot Y equals
178
00:15:14,780 --> 00:15:23,450
two, let's say four, then in this case you will see that also the X value will be change will be changed
179
00:15:23,450 --> 00:15:26,270
since that's the same region in memory.
180
00:15:26,270 --> 00:15:31,690
So let's say after a change union point equals to this value.
181
00:15:31,700 --> 00:15:32,870
So let's see what happens.
182
00:15:33,380 --> 00:15:34,490
So there you go.
183
00:15:34,640 --> 00:15:36,740
Struct point five seven three three.
184
00:15:36,740 --> 00:15:38,930
And then you changed after change.
185
00:15:39,260 --> 00:15:42,800
Union point equals to four point four.
186
00:15:42,950 --> 00:15:44,240
OK, is that clear?
187
00:15:45,290 --> 00:15:53,180
So that means that here in this region we would have like the value of four guys.
188
00:15:54,290 --> 00:15:54,840
Awesome.
189
00:15:55,520 --> 00:15:59,780
So but what will happen basically about the struct.
190
00:15:59,780 --> 00:16:05,540
OK, so let's say we also would have changed like point one Y equals to ten.
191
00:16:06,050 --> 00:16:08,540
Will it also be the same for struct.
192
00:16:08,540 --> 00:16:11,630
Will also both of its values will be changed.
193
00:16:11,870 --> 00:16:12,880
What do you think guys.
194
00:16:13,070 --> 00:16:14,900
Let me know to take a second.
195
00:16:15,230 --> 00:16:16,010
Think about it.
196
00:16:16,010 --> 00:16:22,430
And let's say after a change struct point equals two point one point one does point.
197
00:16:22,430 --> 00:16:23,210
Wonder why.
198
00:16:24,320 --> 00:16:25,630
So what do you think.
199
00:16:26,150 --> 00:16:28,260
What do you think?
200
00:16:29,030 --> 00:16:33,770
So that's basically all is basically build and run it.
201
00:16:34,860 --> 00:16:42,080
And there you go, you can see that only the value of point one dot y has been changed.
202
00:16:42,160 --> 00:16:50,760
OK, you can see that because they are totally separated or regions in memory and they do not affect
203
00:16:50,760 --> 00:16:51,560
one another.
204
00:16:51,900 --> 00:16:53,960
So that's a good thing to know.
205
00:16:54,450 --> 00:17:00,310
That's basically mainly the difference between unions and struct.
206
00:17:02,040 --> 00:17:10,140
So I hope this quick introduction, weak explanation is clear to you guys, and if you still have any
207
00:17:10,140 --> 00:17:16,740
questions or if you want me to make, I don't know, some more descriptive summary perhaps about our
208
00:17:16,860 --> 00:17:22,350
own advantages and disadvantages of using one thing over the other.
209
00:17:22,740 --> 00:17:29,610
So please let me know and hopefully we will see each other again in additional videos.
210
00:17:29,620 --> 00:17:33,930
So until then, have a great time and I wish you the best of luck.
211
00:17:34,140 --> 00:17:34,610
Bye bye.
20287
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.