1 00:00:01,340 --> 00:00:04,660 Memory and the C plus plus standard library here. 2 00:00:04,670 --> 00:00:11,960 This C plus plus standard library provides various classes to allow you manipulate collection of objects. 3 00:00:11,990 --> 00:00:18,680 These classes called the STL Standard Template Library. 4 00:00:18,680 --> 00:00:26,990 So this provides a standard way to insert items into collection objects and ways to access the items 5 00:00:26,990 --> 00:00:29,720 and iterate through the entire collections. 6 00:00:29,720 --> 00:00:31,460 This is called the Iterators. 7 00:00:31,460 --> 00:00:41,090 Here, the STL defines collections classes that are implemented as stacks or as a vectors with a random 8 00:00:41,090 --> 00:00:41,540 access. 9 00:00:41,540 --> 00:00:45,560 Here, these classes will be covered in depth in the next lecture. 10 00:00:45,560 --> 00:00:53,780 So in this section we will limit the discussion to just two classes that behave like C plus plus built 11 00:00:53,780 --> 00:00:54,740 in arrays. 12 00:00:54,770 --> 00:00:58,850 The first, let's begin with the standard library arrays here. 13 00:00:58,880 --> 00:01:09,620 The standard library arrays provides two containers that given random access via an indexer to the data. 14 00:01:09,620 --> 00:01:17,000 So these two containers also allows you to access the underlying memory, since they guarantee to store 15 00:01:17,000 --> 00:01:20,420 the items sequentially and contiguous in memory. 16 00:01:20,420 --> 00:01:24,800 And they can be used when you are required to provide a pointer to a buffer. 17 00:01:24,800 --> 00:01:32,330 So these two types are both templates, which means that you can use them to hold built in in the custom 18 00:01:32,330 --> 00:01:32,810 types. 19 00:01:32,810 --> 00:01:39,650 So these two collection classes are array and vector here. 20 00:01:41,950 --> 00:01:42,250 Yeah. 21 00:01:42,520 --> 00:01:43,960 So these are. 22 00:01:45,090 --> 00:01:48,360 These are the Wilton standard. 23 00:01:49,270 --> 00:01:50,290 Library. 24 00:01:51,230 --> 00:01:51,860 All right. 25 00:01:52,340 --> 00:01:57,020 So we can also use the stack based array class in C plus plus. 26 00:01:57,140 --> 00:02:05,360 Here the array class is defined in the array here, like that array header file here. 27 00:02:05,780 --> 00:02:12,680 So the class allows you to create fixed size arrays on the stack and with the built in arrays, they 28 00:02:12,680 --> 00:02:16,940 cannot shrink or expand at runtime. 29 00:02:16,940 --> 00:02:23,720 So since they are allocated on the stack, they do not require a call to a memory allocator at runtime. 30 00:02:23,720 --> 00:02:27,260 But clearly they should be smaller than the stack frame size. 31 00:02:27,260 --> 00:02:35,510 So this means that an array is a good choice for small array of items, so the size of an array must 32 00:02:35,510 --> 00:02:43,040 be known at compile time and it is passed as a template parameter like that. 33 00:02:43,040 --> 00:02:47,600 Here, for example, let's include a include array here. 34 00:02:50,010 --> 00:02:53,460 Ouray and Ouray. 35 00:02:54,530 --> 00:02:55,210 Here. 36 00:02:55,220 --> 00:02:57,920 Integer four and here. 37 00:02:57,920 --> 00:02:59,780 One, two, three, four. 38 00:03:00,790 --> 00:03:08,530 So in this code, the first template parameter in the angled braces is a type of each item in the array. 39 00:03:08,530 --> 00:03:12,520 And the second parameter is the number of items. 40 00:03:12,520 --> 00:03:21,670 So here number of items and the type of the each item in the array here. 41 00:03:21,670 --> 00:03:28,990 So this code initializes the array with an initialized list, but note that you still have to provide 42 00:03:28,990 --> 00:03:36,280 the size of an array in the template here and this object will work like a built in array or indeed 43 00:03:36,280 --> 00:03:40,450 any of the standard library containers with the ranged loop here. 44 00:03:40,450 --> 00:03:42,490 So let's create a range loop here. 45 00:03:42,790 --> 00:03:47,470 Integer E and here we created the array here. 46 00:03:47,470 --> 00:03:49,150 Make it here. 47 00:03:52,640 --> 00:03:57,870 Right here and we're going to create an array integer. 48 00:04:08,210 --> 00:04:08,510 Here. 49 00:04:10,360 --> 00:04:13,430 Firstly, we're going to create the array like that. 50 00:04:19,000 --> 00:04:22,750 Here and we're going to pass iterate through this array here. 51 00:04:26,310 --> 00:04:31,410 And each time we're going to print this on the screen. 52 00:04:34,300 --> 00:04:38,410 We have to import and use the namespace here. 53 00:04:40,460 --> 00:04:42,830 And print it out. 54 00:04:43,810 --> 00:04:47,080 As you can see, we printed our variables here. 55 00:04:47,110 --> 00:04:53,800 The reason is that our array implements the begin and end function that are required for this syntax. 56 00:04:53,800 --> 00:04:58,840 So you can also use this like that here, for example. 57 00:04:58,840 --> 00:05:16,450 INT for integer E zero oops, integer e equals zero, while e is less than r dot size here and plus 58 00:05:16,450 --> 00:05:20,900 plus E here and C out here. 59 00:05:21,220 --> 00:05:29,410 Our array here, our error E and n line here. 60 00:05:29,410 --> 00:05:32,230 We can write it also like that. 61 00:05:32,230 --> 00:05:33,940 It's the same output here. 62 00:05:33,970 --> 00:05:35,440 The size function here. 63 00:05:35,440 --> 00:05:41,740 As you can see here, we return the size of the array and the square braces. 64 00:05:41,740 --> 00:05:49,550 Indexer gives the random access to the members of the array so you can access the memory outside of 65 00:05:49,550 --> 00:05:51,220 the bounds of the array. 66 00:05:51,230 --> 00:06:00,170 So for the previously defined array that has four members you can access like this array r ten. 67 00:06:00,170 --> 00:06:05,360 So this may cause the unexpected behavior at runtime. 68 00:06:05,360 --> 00:06:06,410 Let's try it out. 69 00:06:07,300 --> 00:06:09,780 As you can see here, there's just a reprinted. 70 00:06:09,790 --> 00:06:11,260 There's a bunch of zeros. 71 00:06:12,060 --> 00:06:12,780 Here. 72 00:06:12,780 --> 00:06:19,720 So or this may cause unexpected behavior or even some kind of memory fault. 73 00:06:19,740 --> 00:06:27,960 So to guard against this, the class provides a function F which will perform, uh, perform a range 74 00:06:27,960 --> 00:06:28,340 check. 75 00:06:28,350 --> 00:06:37,830 And if the index is out of range, the class will throw the cplusplus exception, which, uh, here. 76 00:06:39,250 --> 00:06:39,580 Here. 77 00:06:40,920 --> 00:06:43,020 C plus plus exception named. 78 00:06:43,050 --> 00:06:45,390 Out of range here. 79 00:06:45,810 --> 00:06:49,020 So the main advantage of using an array. 80 00:06:49,410 --> 00:06:56,610 Array object is that you get compile time checks to see if you are inadvertently passing the object 81 00:06:56,610 --> 00:06:59,520 to a function as a dump pointer here. 82 00:06:59,910 --> 00:07:02,140 Let's make another example here. 83 00:07:02,160 --> 00:07:03,030 Void. 84 00:07:04,110 --> 00:07:04,500 Here. 85 00:07:05,960 --> 00:07:09,410 Void use an init. 86 00:07:10,180 --> 00:07:12,100 An integer pointer here. 87 00:07:12,400 --> 00:07:17,620 So at runtime the function does not know the size of the buffer passed to it. 88 00:07:17,620 --> 00:07:26,080 And in this case, the documentation says that you must pass a buffer with ten integer types variables. 89 00:07:26,080 --> 00:07:34,300 But as we see in C plus plus allows you allows a built in array to be used as a pointer. 90 00:07:34,300 --> 00:07:36,850 So we can do it like that. 91 00:07:37,210 --> 00:07:39,340 In practice use. 92 00:07:41,010 --> 00:07:41,730 Ten. 93 00:07:42,960 --> 00:07:43,830 Ah one. 94 00:07:43,830 --> 00:07:44,610 So. 95 00:07:45,510 --> 00:07:45,810 Oops. 96 00:07:49,500 --> 00:07:49,800 Here. 97 00:07:49,980 --> 00:07:55,410 Actually, let's create another array for this example and make comment out. 98 00:07:56,650 --> 00:07:58,540 Comment it in here. 99 00:07:59,440 --> 00:07:59,940 Okay. 100 00:07:59,950 --> 00:08:01,090 Integer array. 101 00:08:02,490 --> 00:08:02,880 Here. 102 00:08:02,880 --> 00:08:04,710 And one, two, three, four. 103 00:08:04,980 --> 00:08:07,650 This contains same variables as previous arrays. 104 00:08:08,040 --> 00:08:08,970 Here. 105 00:08:10,100 --> 00:08:17,050 And then this will read the past, the end of the buffer here. 106 00:08:17,060 --> 00:08:20,570 As you can see here, we got an exception. 107 00:08:20,570 --> 00:08:28,550 So this is no compiler checker nor any runtime check to um, catch this error. 108 00:08:28,760 --> 00:08:36,680 But there is the array class will not allow such an error to happen because there is no automatic conversion 109 00:08:36,680 --> 00:08:39,530 into a dump pointer here. 110 00:08:39,530 --> 00:08:48,320 So if you insist in obtaining a dump pointer, you can do this and be guaranteed to have access to data 111 00:08:48,860 --> 00:08:53,360 as a contiguous block of memory where the items stored sequentially. 112 00:08:53,720 --> 00:08:55,850 So you can do it like that. 113 00:08:59,460 --> 00:08:59,760 Here. 114 00:09:00,860 --> 00:09:01,880 Let's compile it. 115 00:09:04,250 --> 00:09:06,710 And, uh, here. 116 00:09:10,310 --> 00:09:14,690 We got Elder turned one Subcommand filed. 117 00:09:15,380 --> 00:09:20,750 That's why you don't have to do this like that with the dump pointers here. 118 00:09:20,960 --> 00:09:23,720 And we can. 119 00:09:25,780 --> 00:09:27,620 To test it like that here. 120 00:09:28,820 --> 00:09:30,500 Books are one. 121 00:09:31,860 --> 00:09:33,960 And data here. 122 00:09:38,330 --> 00:09:43,280 And as you can see here, this won't compile either. 123 00:09:43,280 --> 00:09:48,860 So this is not the correct, correct coding style here. 124 00:09:48,860 --> 00:09:56,030 And you will learn more about and how to correct this syntax in next lecture.