1
00:00:00,490 --> 00:00:06,350
Cplusplus inherits from a C the idea of argument list.

2
00:00:06,370 --> 00:00:11,470
So to do this you use the ellipsis syntax like this here.

3
00:00:13,320 --> 00:00:14,280
Three dots.

4
00:00:14,290 --> 00:00:21,730
So as the last parameter to indicate that the color can provide zero or more parameters.

5
00:00:21,730 --> 00:00:28,030
So the compiler will check how the function is called and will allocate space on the stack for these

6
00:00:28,060 --> 00:00:29,080
extra parameters.

7
00:00:29,080 --> 00:00:43,160
So to access the extra parameters, your code must include the header file here include include zstd

8
00:00:43,810 --> 00:00:47,050
i g here as you can see here.

9
00:00:47,050 --> 00:00:55,210
So this your code must include the uh, this header file which has macros that you can use to extract

10
00:00:55,210 --> 00:00:57,910
the extra parameters of the stack.

11
00:00:57,910 --> 00:01:04,420
So this is inherently type unsafe because the compiler cannot check that that parameters that the function

12
00:01:04,420 --> 00:01:10,900
will get of the stack at runtime will be the same type as the parameters put on the stack by the calling

13
00:01:10,900 --> 00:01:11,140
code.

14
00:01:11,140 --> 00:01:21,860
For example, let's make another function here named some integer, some integer first and after with

15
00:01:21,860 --> 00:01:23,150
the comma after the comma.

16
00:01:23,180 --> 00:01:26,210
Operator We're going to paste the three dots here.

17
00:01:26,210 --> 00:01:30,440
And in this in this, some function, we're going to create an integer.

18
00:01:32,400 --> 00:01:34,020
Integer sum zero.

19
00:01:34,320 --> 00:01:37,110
Var list here arguments.

20
00:01:37,960 --> 00:01:39,490
Verse start here.

21
00:01:40,030 --> 00:01:41,770
We're going to this takes two arguments.

22
00:01:41,770 --> 00:01:45,970
The first is arguments and we're going to last here.

23
00:01:45,970 --> 00:01:50,230
We're going to pass the first and integer is first.

24
00:01:51,090 --> 00:01:59,550
Enter here first, while the E here, while E is not equal to minus one.

25
00:01:59,580 --> 00:02:09,480
Then the sum here increment a by one here one increment by one.

26
00:02:10,320 --> 00:02:17,490
And equals to f a arguments args here.

27
00:02:25,780 --> 00:02:26,140
Here.

28
00:02:26,140 --> 00:02:29,800
And after that, we're going to use the VA.

29
00:02:30,110 --> 00:02:32,560
VA and.

30
00:02:33,500 --> 00:02:37,820
Pass these arguments here and return the sum lastly here.

31
00:02:37,820 --> 00:02:43,550
So the definition of the function must have at least one parameter so that the macros work here.

32
00:02:44,180 --> 00:02:49,910
In this case, the parameter is called the first, and it's important that your code leaves this stack

33
00:02:49,910 --> 00:02:51,440
in a consistent state.

34
00:02:51,440 --> 00:03:00,260
And this is carried out using a variable named Val type here named arguments that Val has the list type

35
00:03:00,290 --> 00:03:00,470
here.

36
00:03:00,470 --> 00:03:07,750
So this variable is initialized at the beginning of the function by calling the vastart here macro via

37
00:03:07,760 --> 00:03:16,070
start macro and the stack is restored to its previous state at the end of the function by calling the

38
00:03:16,070 --> 00:03:18,890
var and macro here.

39
00:03:18,890 --> 00:03:26,600
So the code in this function simply iterates through the argument list and maintains a sum and the loop

40
00:03:26,600 --> 00:03:36,150
here finishes when the parameter has the value of minus one and there are no macros, there are no macros

41
00:03:36,840 --> 00:03:41,310
to give an indication of the type of the parameter on the stack.

42
00:03:41,310 --> 00:03:52,140
So your code has to assume the type of the variable to provide the desired type with the var args here.

43
00:03:53,310 --> 00:03:57,210
Var arg macro here.

44
00:03:57,210 --> 00:03:59,820
As you can see here we are macro here.

45
00:03:59,820 --> 00:04:04,290
So in this example the vendor var arg is called assuming.

46
00:04:04,290 --> 00:04:12,020
Then the A is assuming that the every parameter on the stack has has type of integer here.

47
00:04:12,030 --> 00:04:19,860
So once our parameters has been read of the stack, the in this code here, we're going to call the

48
00:04:20,790 --> 00:04:26,040
previous code that we've write it here C out some minus one.

49
00:04:26,940 --> 00:04:29,550
And of course end line.

50
00:04:29,820 --> 00:04:32,010
See out some.

51
00:04:33,450 --> 00:04:44,790
Uh, minus one, minus six, minus six, minus five, minus four, minus three, minus two and less

52
00:04:44,790 --> 00:04:45,900
than minus one.

53
00:04:46,950 --> 00:04:52,740
And end line here and see out some.

54
00:04:54,270 --> 00:04:56,100
And here.

55
00:04:59,190 --> 00:05:01,590
And lastly minus one here.

56
00:05:01,800 --> 00:05:05,940
And this gonna end line here so.

57
00:05:08,390 --> 00:05:09,260
Here.

58
00:05:09,950 --> 00:05:11,360
Firstly, we're going to.

59
00:05:12,170 --> 00:05:12,680
Okay.

60
00:05:13,610 --> 00:05:19,310
So here, once parameters have been has been read off the stack.

61
00:05:19,340 --> 00:05:25,630
The code calls var and rear before returning to.

62
00:05:26,420 --> 00:05:29,120
Before returning to the sum here, as you can see here.

63
00:05:29,850 --> 00:05:32,450
Uh, so the function can be called like this.

64
00:05:32,480 --> 00:05:34,640
In this case, we're going to get the zero.

65
00:05:34,640 --> 00:05:38,660
And in this case, we're going to get the -12.

66
00:05:38,780 --> 00:05:42,920
And in this case, we're going to get a 16 here.

67
00:05:44,500 --> 00:05:52,810
So since the minus one is used to indicate that the end of the list, it means that the sum of zero

68
00:05:52,810 --> 00:05:54,790
number of parameters.

69
00:05:54,790 --> 00:06:00,910
So you have to pass at least one parameters, as you can see here.

70
00:06:00,910 --> 00:06:02,080
So.

71
00:06:03,450 --> 00:06:06,060
And that is the minus one here.

72
00:06:06,060 --> 00:06:10,310
In addition, the second line shows the here's the second line.

73
00:06:10,320 --> 00:06:19,050
This shows the, um, the second line shows that you have a problem if you are passing a list of negative

74
00:06:19,050 --> 00:06:20,220
numbers here.

75
00:06:21,850 --> 00:06:27,580
So in this case, the minus one cannot be a parameter here.

76
00:06:27,580 --> 00:06:36,220
So the problem could be addressed in this implementation by choosing another marker value.

77
00:06:36,220 --> 00:06:44,530
So another implementation could skip that the use of marker for the end of the list and instead of first

78
00:06:44,530 --> 00:06:48,760
required argument to give the count of parameters that follow here.

79
00:06:48,760 --> 00:06:57,100
So now while we're going to change, we're going to write it actually from zero to some equals zero.

80
00:06:57,130 --> 00:07:00,550
Var list args v v a start.

81
00:07:00,580 --> 00:07:02,620
We're going to first pass the.

82
00:07:03,420 --> 00:07:04,050
Arguments.

83
00:07:04,050 --> 00:07:11,220
And here we're going to change this name to count and count here.

84
00:07:11,220 --> 00:07:19,650
And while we're going to change this while code here, the while the count is count here.

85
00:07:20,470 --> 00:07:21,580
Integer.

86
00:07:29,300 --> 00:07:34,540
And here integer the var args here.

87
00:07:37,880 --> 00:07:38,750
Integer.

88
00:07:40,250 --> 00:07:41,420
Here, as you can see.

89
00:07:41,450 --> 00:07:42,110
Count.

90
00:07:42,260 --> 00:07:42,920
Count.

91
00:07:43,070 --> 00:07:43,820
Oh, sorry.

92
00:07:46,530 --> 00:07:47,450
Count here.

93
00:07:47,460 --> 00:07:52,860
So and lastly, the sum of E here.

94
00:07:53,130 --> 00:07:56,940
And after that, we're going to firstly end the arguments here.

95
00:07:56,970 --> 00:08:00,870
Arguments and then return the sum.

96
00:08:02,080 --> 00:08:03,570
Return the sum here.

97
00:08:03,580 --> 00:08:05,380
So this time.

98
00:08:07,220 --> 00:08:09,380
Here this time.

99
00:08:10,560 --> 00:08:12,360
The first value here.

100
00:08:12,390 --> 00:08:17,060
The first value is the number of arguments that follows.

101
00:08:17,100 --> 00:08:23,970
And so the routine will extract this number of the integers of the stack and sum them here.

102
00:08:23,970 --> 00:08:31,230
So this code is called when we call this this code, we're going to get the zero if we call this.

103
00:08:32,050 --> 00:08:34,840
And then I get -21.

104
00:08:35,020 --> 00:08:39,910
And if we call this, we're going to get the 60 here.

105
00:08:39,910 --> 00:08:42,970
So let's make it run.

106
00:08:42,970 --> 00:08:46,450
And as you can see here, oops, we got an error here.

107
00:08:46,450 --> 00:08:54,040
So this is no convention for how to handle the determining how many parameters have been passed here.

108
00:08:54,040 --> 00:08:59,080
So the return assumes here, the routine assumes we have to see count.

109
00:08:59,740 --> 00:09:00,370
Okay.

110
00:09:01,400 --> 00:09:03,650
Here and let's run it again.

111
00:09:04,670 --> 00:09:05,150
Here.

112
00:09:13,480 --> 00:09:14,170
Here.

113
00:09:14,170 --> 00:09:20,140
So the routine assumes that the ever items on the stack is an integer.

114
00:09:20,140 --> 00:09:25,180
But there is no information about this in the prototype of the function here.

115
00:09:25,180 --> 00:09:33,430
So the compiler cannot do type checking on the parameters actually used to the call function here.

116
00:09:33,430 --> 00:09:39,340
If the caller provides a parameter of different type, the wrong number of bytes should be read off

117
00:09:39,340 --> 00:09:40,240
the stack here.

118
00:09:40,240 --> 00:09:42,700
So for example, the sum.

119
00:09:43,830 --> 00:09:46,020
And, uh, like this, for example.

120
00:09:46,050 --> 00:09:48,240
See out some.

121
00:09:49,000 --> 00:09:49,690
Here.

122
00:09:49,720 --> 00:09:53,570
310 dot and 2030.

123
00:09:53,950 --> 00:09:59,500
So it's easy to press both the comma and period keys here at the same time.

124
00:09:59,500 --> 00:10:03,490
And this has happened after trying the temp parameter.

125
00:10:03,490 --> 00:10:11,350
So the period means that the ten here, this means here ten is a double and this compiler puts a double

126
00:10:11,350 --> 00:10:12,610
value on the stack here.

127
00:10:12,610 --> 00:10:20,020
So in this case, the first is integer and double integer integer here, integer integer here.

128
00:10:20,260 --> 00:10:21,400
So.

129
00:10:22,390 --> 00:10:27,070
The compiler is going to put a double value on the stack.

130
00:10:27,070 --> 00:10:32,430
So when the function reads values of the stack with the var argument.

131
00:10:32,660 --> 00:10:41,830
Var argument macro it will read the eight bytes double as two byte integer values and for code produced

132
00:10:41,860 --> 00:10:49,060
by this compiler, the results in a total sum of just a random number here.

133
00:10:49,060 --> 00:10:55,000
So this illustrates the type unsafe aspect of arguments list here.

134
00:10:55,000 --> 00:11:00,280
So you cannot get the compiler to do the type checks of the parameter passed to the function here.

135
00:11:00,280 --> 00:11:07,690
So if your function has different types passed to it, then you have to implement some mechanism to

136
00:11:07,690 --> 00:11:11,380
determine what those parameters are.
