1
00:00:01,340 --> 00:00:03,580
Unions in Cplusplus.

2
00:00:03,620 --> 00:00:11,640
So unions, as a cousin of the pod that puts all of its member in the same place.

3
00:00:11,660 --> 00:00:20,120
So you can think of unions as a different views or interpretations of a block of a memory so they can

4
00:00:20,120 --> 00:00:26,770
be useful in some low level situations, such as when marshalling structures that must be consistent

5
00:00:26,780 --> 00:00:27,560
across architectures.

6
00:00:27,560 --> 00:00:36,230
So dealing with the type checking issues related to C, Cplusplus, Interoperation and even packing

7
00:00:36,230 --> 00:00:44,210
Bitfields So let's create a union here, a union here, union variant here variant.

8
00:00:45,810 --> 00:00:49,410
As you can see here, this is this definition is same as struct.

9
00:00:49,410 --> 00:00:51,210
So in previous lecture we defined.

10
00:00:51,240 --> 00:00:53,670
Struct and create an example from it.

11
00:00:54,210 --> 00:00:57,240
And so let's create a character.

12
00:00:57,240 --> 00:01:06,150
My string here ten then integer my integer here.

13
00:01:07,640 --> 00:01:08,690
A double.

14
00:01:09,610 --> 00:01:12,310
Double my.

15
00:01:12,920 --> 00:01:15,100
My double, for example.

16
00:01:16,160 --> 00:01:27,920
So then the union here variant can be interpreted as my string ten an integer or a double.

17
00:01:28,160 --> 00:01:32,810
So it takes up only as much memory as it largest member.

18
00:01:32,810 --> 00:01:34,520
Probably a string here.

19
00:01:34,520 --> 00:01:36,590
My string in this case.

20
00:01:36,590 --> 00:01:43,400
So you can use that operator to specify a union's interpretation.

21
00:01:43,400 --> 00:01:51,170
Syntactically this looks like this looks like accessing a member of Pod, but it's completely different

22
00:01:51,170 --> 00:01:52,190
under the hood here.

23
00:01:52,190 --> 00:01:55,640
So let's create an variant here.

24
00:01:55,670 --> 00:01:57,260
Variant V.

25
00:01:59,040 --> 00:02:07,680
Here we that integer, my integer, for example, 32 and printf.

26
00:02:08,520 --> 00:02:13,130
The answer here answer.

27
00:02:14,030 --> 00:02:14,360
Here.

28
00:02:15,850 --> 00:02:18,670
We that integer.

29
00:02:18,700 --> 00:02:19,870
My integer.

30
00:02:19,870 --> 00:02:23,260
And let's define the that floating point here.

31
00:02:23,260 --> 00:02:26,320
We got my double.

32
00:02:26,350 --> 00:02:27,910
My double.

33
00:02:27,910 --> 00:02:28,600
Yes.

34
00:02:28,810 --> 00:02:39,100
And here two point or three point here this is the double and then print f.

35
00:02:42,550 --> 00:02:44,290
My double.

36
00:02:44,320 --> 00:02:46,900
Or let's make it my integer.

37
00:02:46,900 --> 00:02:50,050
My union integer.

38
00:02:51,070 --> 00:02:55,420
And my union double.

39
00:02:56,470 --> 00:02:58,540
Here or here?

40
00:02:59,560 --> 00:02:59,880
Here.

41
00:02:59,890 --> 00:03:03,670
My union double D and.

42
00:03:06,240 --> 00:03:07,450
We that.

43
00:03:10,240 --> 00:03:11,140
My double.

44
00:03:12,880 --> 00:03:14,650
And then here.

45
00:03:17,200 --> 00:03:17,830
Print test.

46
00:03:20,260 --> 00:03:23,620
My union integer.

47
00:03:25,250 --> 00:03:27,070
Uh, print here.

48
00:03:27,070 --> 00:03:29,620
My union integer two, for example.

49
00:03:30,190 --> 00:03:34,930
Here d v.my integer.

50
00:03:34,930 --> 00:03:43,120
Actually, we have we used the wrong format specifier here we have to use F okay, so.

51
00:03:45,640 --> 00:03:47,530
Let's run our program now.

52
00:03:47,860 --> 00:03:50,410
So let's run here.

53
00:03:51,810 --> 00:03:52,010
Oops.

54
00:03:52,260 --> 00:03:54,750
Actually, let's add the new lines here.

55
00:03:55,380 --> 00:03:56,110
Okay.

56
00:03:56,130 --> 00:03:57,300
New line.

57
00:03:59,500 --> 00:03:59,980
Oops.

58
00:04:04,280 --> 00:04:05,660
New line and new line.

59
00:04:06,580 --> 00:04:07,300
Here.

60
00:04:08,860 --> 00:04:13,260
As you can see, this is the weird answer we got here.

61
00:04:13,270 --> 00:04:17,230
So firstly we declared variant V.

62
00:04:17,800 --> 00:04:26,830
Next we interpret we as an integer so and set its value to 32 here.

63
00:04:27,700 --> 00:04:29,290
And then print it.

64
00:04:30,260 --> 00:04:34,460
So you hear then reinterpret the.

65
00:04:35,410 --> 00:04:40,660
As a float and reassign its value here.

66
00:04:43,540 --> 00:04:52,900
So you printed the the to the console and it appears well as you as we've righted it so far so good

67
00:04:52,900 --> 00:04:53,200
here.

68
00:04:53,200 --> 00:05:01,900
So disaster strikes only when you try to interrupt V as an integer again here.

69
00:05:02,630 --> 00:05:07,790
As you can see, our integer is 32 and this gave us some numbers.

70
00:05:08,000 --> 00:05:09,080
So.

71
00:05:10,200 --> 00:05:15,330
When were you clobbered over the original value of V 32?

72
00:05:15,360 --> 00:05:26,250
So when assigning this my double value so we can use this printf here before my double he or let's two

73
00:05:26,250 --> 00:05:28,860
before my double.

74
00:05:29,670 --> 00:05:34,230
Here and then after my double.

75
00:05:37,180 --> 00:05:42,820
As you can see here before, my double is almost everything is completely fine.

76
00:05:42,820 --> 00:05:46,840
We got the same number, we printed the same number we assign.

77
00:05:46,840 --> 00:05:51,190
And after that we just got a completely different value.

78
00:05:52,230 --> 00:05:53,610
And disaster strike.

79
00:05:53,850 --> 00:05:56,340
So that's the main problem with unions.

80
00:05:56,370 --> 00:06:02,940
It's up to you to keep track of which interpretation is appropriate so the compiler won't help you.

81
00:06:03,090 --> 00:06:10,830
You should avoid using unions in all but the rarest of cases, and you won't see them in this course

82
00:06:10,830 --> 00:06:11,400
here.

83
00:06:12,750 --> 00:06:21,510
Maybe in later lectures because unions has a very narrow use case in C plus plus just the daily programming.

84
00:06:26,070 --> 00:06:26,700
Here.
