1 00:00:00,530 --> 00:00:06,460 C++ inherits from a C the idea of argument lists. 2 00:00:06,470 --> 00:00:11,510 So to do this you use the ellipses syntax like this here. 3 00:00:13,430 --> 00:00:14,420 Three dots. 4 00:00:14,420 --> 00:00:22,700 So as the last parameter to indicate that the color can provide zero or more parameters, So the compiler 5 00:00:22,700 --> 00:00:29,120 will check how the function is called and will allocate space on the stack for these extra parameters. 6 00:00:29,120 --> 00:00:38,570 So to access the extra parameters, you code must include the header file here. 7 00:00:39,140 --> 00:00:42,500 Include include c. 8 00:00:42,500 --> 00:00:47,120 S, t, the i, r g here as you can see here. 9 00:00:47,120 --> 00:00:55,250 So this u code must include the, uh, this header file which has macros that you can use to extract 10 00:00:55,250 --> 00:00:57,980 the extra parameters of the stack. 11 00:00:57,980 --> 00:01:04,490 So this is inherently unsafe because the compiler cannot check the that parameters that the function 12 00:01:04,490 --> 00:01:11,000 will get over the stack at runtime will be the same type as the parameters put on the stack by the calling 13 00:01:11,000 --> 00:01:11,210 code. 14 00:01:11,210 --> 00:01:21,560 For example, let's make an another function here named some integer, some integer first and after. 15 00:01:21,560 --> 00:01:23,540 With the comma, after the comma operator. 16 00:01:23,540 --> 00:01:26,210 We're going to page the three dots here. 17 00:01:26,210 --> 00:01:30,380 And in this in this sum function, we're going to create an integer. 18 00:01:32,530 --> 00:01:32,890 Integer. 19 00:01:32,890 --> 00:01:34,060 Sum zero. 20 00:01:34,810 --> 00:01:35,920 List here. 21 00:01:36,190 --> 00:01:37,180 Arguments. 22 00:01:38,090 --> 00:01:39,530 Why start here? 23 00:01:40,130 --> 00:01:41,840 We're going to this takes two arguments. 24 00:01:41,840 --> 00:01:45,210 The first is arguments, and we're going to last. 25 00:01:46,040 --> 00:01:50,270 We're going to pass the first and integer E first. 26 00:01:51,160 --> 00:02:04,090 Ensure ea here first while the EA here while e is not equal to minus one, then the sum here increment 27 00:02:04,090 --> 00:02:09,460 E by one one increment side by one. 28 00:02:10,390 --> 00:02:15,520 And equals to have a arguments. 29 00:02:16,120 --> 00:02:17,500 Alex here. 30 00:02:25,880 --> 00:02:26,240 Here. 31 00:02:26,240 --> 00:02:32,600 And after that we're going to use the f, a, v, a, and. 32 00:02:33,570 --> 00:02:35,220 Pass this outgoings here. 33 00:02:35,550 --> 00:02:37,880 Return the sum lastly here. 34 00:02:37,890 --> 00:02:43,560 So the definition of the function must have at least one parameter so that the macros work here. 35 00:02:44,370 --> 00:02:49,950 In this case the parameter is called the first, and it's important that your code leaves this stack 36 00:02:49,950 --> 00:02:55,740 in a consistent state and this is carried out using a variable named Val. 37 00:02:55,740 --> 00:03:00,540 This type here named arguments that Val has, the file list type here. 38 00:03:00,540 --> 00:03:07,470 So this variable is initialized at the beginning of the function by calling the V a start here macro 39 00:03:07,500 --> 00:03:15,750 v I start macro and the stack is restored to its previous state at the end of the function by calling 40 00:03:15,750 --> 00:03:18,930 the v a and macro here. 41 00:03:18,930 --> 00:03:26,280 So the code in this function simply iterates through the argument list and maintains a sum, and the 42 00:03:26,280 --> 00:03:31,920 loop here finishes when the parameter has the value of minus one. 43 00:03:32,160 --> 00:03:40,710 And there are no macros, there are no macros to give an indication of the type of the parameter on 44 00:03:40,710 --> 00:03:41,340 the stack. 45 00:03:41,340 --> 00:03:48,330 So your cut has to assume the type of the variable to provide the desired type. 46 00:03:48,330 --> 00:03:52,140 V the v a x here. 47 00:03:53,470 --> 00:03:57,280 V a r macro here. 48 00:03:57,280 --> 00:03:59,890 As you can see here, we are macro here. 49 00:03:59,890 --> 00:04:07,990 So in this example, the when the v a r is called assuming and it's assuming that the every parameter 50 00:04:07,990 --> 00:04:12,070 on the stack has has type of integer here. 51 00:04:12,100 --> 00:04:19,810 So once our parameter has been read out of the stack, the in this code here, we're going to call the 52 00:04:20,890 --> 00:04:26,110 previous code that we write it here, see out some minus one. 53 00:04:26,980 --> 00:04:32,170 And of course in line C out some. 54 00:04:33,400 --> 00:04:44,380 Uh, minus one, minus six, uh, minus six, minus five, minus four, minus three, minus two and 55 00:04:44,620 --> 00:04:45,880 plus the minus one. 56 00:04:47,100 --> 00:04:52,930 And end line here and see out some. 57 00:04:54,340 --> 00:04:56,200 And here. 58 00:04:59,470 --> 00:05:04,870 Less than minus one here and this corner and line here. 59 00:05:05,050 --> 00:05:06,070 So. 60 00:05:08,640 --> 00:05:09,270 Here. 61 00:05:10,080 --> 00:05:11,400 Firstly, we're going to. 62 00:05:12,240 --> 00:05:12,720 Okay. 63 00:05:13,680 --> 00:05:24,690 So here, once a parameter that has been read off the stack, the code calls V, a, and V before returning 64 00:05:24,720 --> 00:05:29,100 to before returning to the sum here, as you can see here. 65 00:05:30,180 --> 00:05:32,500 So the function can be called like this. 66 00:05:32,520 --> 00:05:34,680 In this case, we're going to get the zero. 67 00:05:34,800 --> 00:05:38,730 And in this case, we're going to get the -12. 68 00:05:39,090 --> 00:05:42,990 And in this case, we're going to get this 16 here. 69 00:05:44,660 --> 00:05:53,660 So since the minus one is used to indicate at the end of the list, it means that the sum of zero number 70 00:05:53,660 --> 00:05:54,860 of parameters. 71 00:05:54,860 --> 00:05:59,090 So you have to pass at least one parameters. 72 00:05:59,120 --> 00:06:00,990 As you can see here. 73 00:06:01,010 --> 00:06:02,210 So. 74 00:06:03,550 --> 00:06:06,130 And that is the minus one here. 75 00:06:06,130 --> 00:06:09,250 In addition, the second line shows the earth. 76 00:06:09,250 --> 00:06:10,420 The second line. 77 00:06:10,450 --> 00:06:19,750 This shows the the second line shows that you have a problem if you are passing a list of negative numbers 78 00:06:19,750 --> 00:06:20,260 here. 79 00:06:21,920 --> 00:06:27,650 So in this case, the minus one cannot be a parameter here. 80 00:06:27,650 --> 00:06:36,290 So the problem could be addressed in this implementation by choosing another market value. 81 00:06:36,290 --> 00:06:43,700 So another implementation called is key that the use of macro for the end of the list and instead of 82 00:06:43,700 --> 00:06:48,810 first required argument to give the kind of parameters that follow here. 83 00:06:48,830 --> 00:06:57,200 So now while we're going to change, we're going to write it actually from zero to sum equals zero. 84 00:06:57,290 --> 00:07:00,680 Well arcs vs v a start. 85 00:07:00,680 --> 00:07:02,630 We're going to first pass the. 86 00:07:03,460 --> 00:07:04,180 Arguments. 87 00:07:04,180 --> 00:07:11,170 And here we're going to change this name to count and count here. 88 00:07:11,410 --> 00:07:19,690 And the while we're going to change this vile code here, the vile the count is count here. 89 00:07:20,600 --> 00:07:21,620 Integer. 90 00:07:29,430 --> 00:07:34,620 And here integer the v a r here. 91 00:07:37,560 --> 00:07:38,640 X integer. 92 00:07:40,350 --> 00:07:41,490 Here, as you can see. 93 00:07:41,490 --> 00:07:42,330 Count. 94 00:07:42,570 --> 00:07:43,290 Count of. 95 00:07:43,290 --> 00:07:43,860 Sorry. 96 00:07:46,570 --> 00:07:47,410 Can't hear. 97 00:07:47,620 --> 00:07:48,820 So. 98 00:07:49,000 --> 00:07:52,840 And lastly, the sum of E here. 99 00:07:53,380 --> 00:08:00,940 And after that, we're going to firstly end the arguments here, arguments and then return the sum. 100 00:08:02,120 --> 00:08:03,640 Return the sum here. 101 00:08:03,650 --> 00:08:05,420 So this time. 102 00:08:07,260 --> 00:08:09,420 Here this time. 103 00:08:10,690 --> 00:08:12,480 The first value here. 104 00:08:12,490 --> 00:08:16,690 The first value is the number of arguments that follow. 105 00:08:16,730 --> 00:08:24,020 Suzanne So the routine will extract this number of the integers of the stack and the sum of them here. 106 00:08:24,060 --> 00:08:29,080 So this code is called when we call this this code, we're going to get this zero. 107 00:08:29,980 --> 00:08:31,270 If we call this. 108 00:08:32,120 --> 00:08:34,940 And then it gets -21. 109 00:08:35,090 --> 00:08:40,000 And if we call this, we're going to get the 60 here. 110 00:08:40,010 --> 00:08:43,100 So let's make it run. 111 00:08:43,130 --> 00:08:46,520 And as you can see here, obviously, we got an error here. 112 00:08:46,550 --> 00:08:53,630 So this is no conviction for how to handle the usually determining how many parameters have been passed 113 00:08:53,630 --> 00:08:54,100 here. 114 00:08:54,110 --> 00:08:59,150 So the return assumes here, the routine assumes we have the C count. 115 00:09:01,560 --> 00:09:03,690 Here, and let's run it again. 116 00:09:04,720 --> 00:09:05,190 There. 117 00:09:13,640 --> 00:09:14,120 Here. 118 00:09:14,390 --> 00:09:22,490 So the routine assumes that the every items on the stack is an integer, but there is there is no information 119 00:09:22,490 --> 00:09:25,250 about is in the prototype of the function here. 120 00:09:25,250 --> 00:09:33,530 So the compiler cannot do type checking on the parameters actually used to the call function here. 121 00:09:33,530 --> 00:09:39,380 If the color provides a parameter of different types, the wrong number of bytes should be read off 122 00:09:39,380 --> 00:09:40,330 the stack here. 123 00:09:40,340 --> 00:09:42,770 So for example, the sum. 124 00:09:43,810 --> 00:09:48,340 And, uh, like this, for example, see out some. 125 00:09:49,130 --> 00:09:53,480 Here three, ten and 2030. 126 00:09:54,170 --> 00:09:59,600 So it's easy to spot the comma and the period case here at the same time. 127 00:09:59,600 --> 00:10:03,560 And this has happened after trying the ten parameter. 128 00:10:03,560 --> 00:10:09,080 So the period means that the ten here, this means here ten is a double. 129 00:10:09,080 --> 00:10:12,680 And this compiler puts a double value on the stack here. 130 00:10:12,680 --> 00:10:20,000 So in this case, the first is integer and double integer integer here, integer integer here. 131 00:10:20,510 --> 00:10:21,560 So. 132 00:10:22,490 --> 00:10:27,110 The compiler is going to put a double value on the stack. 133 00:10:27,110 --> 00:10:35,690 So when the function reads values of the stack with the v a argument, v a argument macro, it will 134 00:10:35,690 --> 00:10:44,270 read the eight byte double as two byte integer values and four code produced by this compiler. 135 00:10:44,360 --> 00:10:49,100 The results in a total sum of just a random number here. 136 00:10:49,100 --> 00:10:55,100 So this illustrates the type on safe aspect of arguments list here. 137 00:10:55,100 --> 00:11:00,350 So you cannot get the compiler to do the type five checks of the parameter path to the function here. 138 00:11:00,350 --> 00:11:08,570 So if your function has different types path to it, then you have to implement some mechanism to determine 139 00:11:08,570 --> 00:11:11,390 what DOS parameters are.