WEBVTT

00:00.490 --> 00:06.350
Cplusplus inherits from a C the idea of argument list.

00:06.370 --> 00:11.470
So to do this you use the ellipsis syntax like this here.

00:13.320 --> 00:14.280
Three dots.

00:14.290 --> 00:21.730
So as the last parameter to indicate that the color can provide zero or more parameters.

00:21.730 --> 00:28.030
So the compiler will check how the function is called and will allocate space on the stack for these

00:28.060 --> 00:29.080
extra parameters.

00:29.080 --> 00:43.160
So to access the extra parameters, your code must include the header file here include include zstd

00:43.810 --> 00:47.050
i g here as you can see here.

00:47.050 --> 00:55.210
So this your code must include the uh, this header file which has macros that you can use to extract

00:55.210 --> 00:57.910
the extra parameters of the stack.

00:57.910 --> 01:04.420
So this is inherently type unsafe because the compiler cannot check that that parameters that the function

01:04.420 --> 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

01:10.900 --> 01:11.140
code.

01:11.140 --> 01:21.860
For example, let's make another function here named some integer, some integer first and after with

01:21.860 --> 01:23.150
the comma after the comma.

01:23.180 --> 01:26.210
Operator We're going to paste the three dots here.

01:26.210 --> 01:30.440
And in this in this, some function, we're going to create an integer.

01:32.400 --> 01:34.020
Integer sum zero.

01:34.320 --> 01:37.110
Var list here arguments.

01:37.960 --> 01:39.490
Verse start here.

01:40.030 --> 01:41.770
We're going to this takes two arguments.

01:41.770 --> 01:45.970
The first is arguments and we're going to last here.

01:45.970 --> 01:50.230
We're going to pass the first and integer is first.

01:51.090 --> 01:59.550
Enter here first, while the E here, while E is not equal to minus one.

01:59.580 --> 02:09.480
Then the sum here increment a by one here one increment by one.

02:10.320 --> 02:17.490
And equals to f a arguments args here.

02:25.780 --> 02:26.140
Here.

02:26.140 --> 02:29.800
And after that, we're going to use the VA.

02:30.110 --> 02:32.560
VA and.

02:33.500 --> 02:37.820
Pass these arguments here and return the sum lastly here.

02:37.820 --> 02:43.550
So the definition of the function must have at least one parameter so that the macros work here.

02:44.180 --> 02:49.910
In this case, the parameter is called the first, and it's important that your code leaves this stack

02:49.910 --> 02:51.440
in a consistent state.

02:51.440 --> 03:00.260
And this is carried out using a variable named Val type here named arguments that Val has the list type

03:00.290 --> 03:00.470
here.

03:00.470 --> 03:07.750
So this variable is initialized at the beginning of the function by calling the vastart here macro via

03:07.760 --> 03:16.070
start macro and the stack is restored to its previous state at the end of the function by calling the

03:16.070 --> 03:18.890
var and macro here.

03:18.890 --> 03:26.600
So the code in this function simply iterates through the argument list and maintains a sum and the loop

03:26.600 --> 03:36.150
here finishes when the parameter has the value of minus one and there are no macros, there are no macros

03:36.840 --> 03:41.310
to give an indication of the type of the parameter on the stack.

03:41.310 --> 03:52.140
So your code has to assume the type of the variable to provide the desired type with the var args here.

03:53.310 --> 03:57.210
Var arg macro here.

03:57.210 --> 03:59.820
As you can see here we are macro here.

03:59.820 --> 04:04.290
So in this example the vendor var arg is called assuming.

04:04.290 --> 04:12.020
Then the A is assuming that the every parameter on the stack has has type of integer here.

04:12.030 --> 04:19.860
So once our parameters has been read of the stack, the in this code here, we're going to call the

04:20.790 --> 04:26.040
previous code that we've write it here C out some minus one.

04:26.940 --> 04:29.550
And of course end line.

04:29.820 --> 04:32.010
See out some.

04:33.450 --> 04:44.790
Uh, minus one, minus six, minus six, minus five, minus four, minus three, minus two and less

04:44.790 --> 04:45.900
than minus one.

04:46.950 --> 04:52.740
And end line here and see out some.

04:54.270 --> 04:56.100
And here.

04:59.190 --> 05:01.590
And lastly minus one here.

05:01.800 --> 05:05.940
And this gonna end line here so.

05:08.390 --> 05:09.260
Here.

05:09.950 --> 05:11.360
Firstly, we're going to.

05:12.170 --> 05:12.680
Okay.

05:13.610 --> 05:19.310
So here, once parameters have been has been read off the stack.

05:19.340 --> 05:25.630
The code calls var and rear before returning to.

05:26.420 --> 05:29.120
Before returning to the sum here, as you can see here.

05:29.850 --> 05:32.450
Uh, so the function can be called like this.

05:32.480 --> 05:34.640
In this case, we're going to get the zero.

05:34.640 --> 05:38.660
And in this case, we're going to get the -12.

05:38.780 --> 05:42.920
And in this case, we're going to get a 16 here.

05:44.500 --> 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

05:52.810 --> 05:54.790
number of parameters.

05:54.790 --> 06:00.910
So you have to pass at least one parameters, as you can see here.

06:00.910 --> 06:02.080
So.

06:03.450 --> 06:06.060
And that is the minus one here.

06:06.060 --> 06:10.310
In addition, the second line shows the here's the second line.

06:10.320 --> 06:19.050
This shows the, um, the second line shows that you have a problem if you are passing a list of negative

06:19.050 --> 06:20.220
numbers here.

06:21.850 --> 06:27.580
So in this case, the minus one cannot be a parameter here.

06:27.580 --> 06:36.220
So the problem could be addressed in this implementation by choosing another marker value.

06:36.220 --> 06:44.530
So another implementation could skip that the use of marker for the end of the list and instead of first

06:44.530 --> 06:48.760
required argument to give the count of parameters that follow here.

06:48.760 --> 06:57.100
So now while we're going to change, we're going to write it actually from zero to some equals zero.

06:57.130 --> 07:00.550
Var list args v v a start.

07:00.580 --> 07:02.620
We're going to first pass the.

07:03.420 --> 07:04.050
Arguments.

07:04.050 --> 07:11.220
And here we're going to change this name to count and count here.

07:11.220 --> 07:19.650
And while we're going to change this while code here, the while the count is count here.

07:20.470 --> 07:21.580
Integer.

07:29.300 --> 07:34.540
And here integer the var args here.

07:37.880 --> 07:38.750
Integer.

07:40.250 --> 07:41.420
Here, as you can see.

07:41.450 --> 07:42.110
Count.

07:42.260 --> 07:42.920
Count.

07:43.070 --> 07:43.820
Oh, sorry.

07:46.530 --> 07:47.450
Count here.

07:47.460 --> 07:52.860
So and lastly, the sum of E here.

07:53.130 --> 07:56.940
And after that, we're going to firstly end the arguments here.

07:56.970 --> 08:00.870
Arguments and then return the sum.

08:02.080 --> 08:03.570
Return the sum here.

08:03.580 --> 08:05.380
So this time.

08:07.220 --> 08:09.380
Here this time.

08:10.560 --> 08:12.360
The first value here.

08:12.390 --> 08:17.060
The first value is the number of arguments that follows.

08:17.100 --> 08:23.970
And so the routine will extract this number of the integers of the stack and sum them here.

08:23.970 --> 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.

08:32.050 --> 08:34.840
And then I get -21.

08:35.020 --> 08:39.910
And if we call this, we're going to get the 60 here.

08:39.910 --> 08:42.970
So let's make it run.

08:42.970 --> 08:46.450
And as you can see here, oops, we got an error here.

08:46.450 --> 08:54.040
So this is no convention for how to handle the determining how many parameters have been passed here.

08:54.040 --> 08:59.080
So the return assumes here, the routine assumes we have to see count.

08:59.740 --> 09:00.370
Okay.

09:01.400 --> 09:03.650
Here and let's run it again.

09:04.670 --> 09:05.150
Here.

09:13.480 --> 09:14.170
Here.

09:14.170 --> 09:20.140
So the routine assumes that the ever items on the stack is an integer.

09:20.140 --> 09:25.180
But there is no information about this in the prototype of the function here.

09:25.180 --> 09:33.430
So the compiler cannot do type checking on the parameters actually used to the call function here.

09:33.430 --> 09:39.340
If the caller provides a parameter of different type, the wrong number of bytes should be read off

09:39.340 --> 09:40.240
the stack here.

09:40.240 --> 09:42.700
So for example, the sum.

09:43.830 --> 09:46.020
And, uh, like this, for example.

09:46.050 --> 09:48.240
See out some.

09:49.000 --> 09:49.690
Here.

09:49.720 --> 09:53.570
310 dot and 2030.

09:53.950 --> 09:59.500
So it's easy to press both the comma and period keys here at the same time.

09:59.500 --> 10:03.490
And this has happened after trying the temp parameter.

10:03.490 --> 10:11.350
So the period means that the ten here, this means here ten is a double and this compiler puts a double

10:11.350 --> 10:12.610
value on the stack here.

10:12.610 --> 10:20.020
So in this case, the first is integer and double integer integer here, integer integer here.

10:20.260 --> 10:21.400
So.

10:22.390 --> 10:27.070
The compiler is going to put a double value on the stack.

10:27.070 --> 10:32.430
So when the function reads values of the stack with the var argument.

10:32.660 --> 10:41.830
Var argument macro it will read the eight bytes double as two byte integer values and for code produced

10:41.860 --> 10:49.060
by this compiler, the results in a total sum of just a random number here.

10:49.060 --> 10:55.000
So this illustrates the type unsafe aspect of arguments list here.

10:55.000 --> 11:00.280
So you cannot get the compiler to do the type checks of the parameter passed to the function here.

11:00.280 --> 11:07.690
So if your function has different types passed to it, then you have to implement some mechanism to

11:07.690 --> 11:11.380
determine what those parameters are.
