1 00:00:00,760 --> 00:00:02,620 Accessing out of bounds. 2 00:00:03,010 --> 00:00:09,700 When you allocate a buffer, whether on the stack or on the free store, you get a pointer. 3 00:00:10,000 --> 00:00:13,090 There is little to stop you from accessing memory. 4 00:00:13,090 --> 00:00:17,710 You have not allocated either before or after the position of the buffer. 5 00:00:17,740 --> 00:00:25,510 This means that when you use pointer arithmetic or index access on arrays that you check carefully that 6 00:00:25,510 --> 00:00:28,900 you are not giving to access the data out of bounds. 7 00:00:28,930 --> 00:00:36,940 Sometimes the error may not be immediately obvious, but now I want to create some code that will show 8 00:00:36,940 --> 00:00:39,250 how this out of bounds works here. 9 00:00:39,430 --> 00:00:40,780 One, two, three, four. 10 00:00:40,780 --> 00:00:41,440 Here. 11 00:00:42,860 --> 00:00:43,370 Okay. 12 00:00:43,370 --> 00:00:46,610 Integer array here and. 13 00:00:47,190 --> 00:00:52,490 Let's make a for loop which this for loop will try to reach the out of bounds. 14 00:00:52,500 --> 00:00:59,090 So this for loop will try to reach the fourth element of this our array element here. 15 00:00:59,100 --> 00:01:07,800 So for make an integer zero here and while integer is less than four. 16 00:01:09,010 --> 00:01:10,090 Uh, plus. 17 00:01:10,090 --> 00:01:10,310 Plus. 18 00:01:10,480 --> 00:01:11,950 Let's make it five, actually. 19 00:01:13,490 --> 00:01:15,410 So here right here. 20 00:01:15,440 --> 00:01:24,410 Or actually make it four because, um, I want to tell something and we'll connect this subject to, 21 00:01:24,410 --> 00:01:29,330 uh, another here plus equals array. 22 00:01:30,410 --> 00:01:33,050 A plus one. 23 00:01:34,710 --> 00:01:35,000 Here. 24 00:01:35,340 --> 00:01:43,380 This is going to be what happens when E equals three here. 25 00:01:44,520 --> 00:01:46,500 So let's print it out. 26 00:01:46,500 --> 00:01:47,190 Actually. 27 00:01:47,220 --> 00:01:48,330 See out. 28 00:01:49,600 --> 00:01:50,440 Okay. 29 00:01:50,890 --> 00:01:52,690 And make it variable. 30 00:01:52,960 --> 00:01:53,500 Integer. 31 00:01:53,530 --> 00:01:54,130 A e. 32 00:01:55,980 --> 00:01:56,550 Here. 33 00:01:56,550 --> 00:02:00,690 Integer A and end line. 34 00:02:03,180 --> 00:02:03,840 Here. 35 00:02:07,090 --> 00:02:15,200 So when you use indexing, you have to keep reminding yourself that arrays are indexed from zero. 36 00:02:15,220 --> 00:02:19,000 Arrays are in index. 37 00:02:20,190 --> 00:02:21,740 From zero. 38 00:02:21,750 --> 00:02:29,190 So last array equals array minus one. 39 00:02:30,180 --> 00:02:31,350 So. 40 00:02:33,000 --> 00:02:41,100 As you can see here, if we increase this to, for example, this 4 to 10, as you can see, we will 41 00:02:41,100 --> 00:02:43,050 get the random numbers here. 42 00:02:46,060 --> 00:02:46,150 The. 43 00:02:46,270 --> 00:02:46,900 And run. 44 00:02:48,290 --> 00:02:48,710 Here. 45 00:02:49,790 --> 00:02:51,470 So now I want to talk about. 46 00:02:51,500 --> 00:02:55,820 Something that pointers have in the allocated memory. 47 00:02:56,210 --> 00:03:02,870 So that's the main reasons of why pointers are, uh, like dangerous. 48 00:03:02,870 --> 00:03:08,100 So this applies to memory allocated on the stack and to memory. 49 00:03:08,120 --> 00:03:09,680 Dynamically allocated. 50 00:03:09,680 --> 00:03:18,470 So now I want to create some function will that will return character here get and. 51 00:03:19,260 --> 00:03:19,830 Here. 52 00:03:20,370 --> 00:03:23,040 This will return the character. 53 00:03:23,070 --> 00:03:24,960 See here. 54 00:03:24,960 --> 00:03:25,920 And. 55 00:03:25,920 --> 00:03:26,760 Hello. 56 00:03:28,110 --> 00:03:28,620 Hello. 57 00:03:29,130 --> 00:03:30,490 And return. 58 00:03:30,510 --> 00:03:31,530 See here. 59 00:03:31,740 --> 00:03:39,390 So this code allocates a buffer of six characters and then initialize it with the five characters of 60 00:03:39,390 --> 00:03:39,840 the string. 61 00:03:39,840 --> 00:03:40,440 Literal. 62 00:03:40,470 --> 00:03:41,010 Hello. 63 00:03:41,010 --> 00:03:41,520 Here. 64 00:03:41,700 --> 00:03:42,240 So. 65 00:03:42,240 --> 00:03:44,160 And this by the six character. 66 00:03:44,160 --> 00:03:46,910 I mean, the last character is null here. 67 00:03:46,920 --> 00:03:48,180 So. 68 00:03:48,180 --> 00:03:52,200 And this null means the termination character in the strings and characters here. 69 00:03:52,200 --> 00:03:59,400 So the problem is that once the function finishes, the stack frame is turned down so that the memory 70 00:03:59,400 --> 00:04:05,910 can be reused and the pointer will point to memory that could be used by something else. 71 00:04:06,090 --> 00:04:13,650 So this error is caused by poor programming here, but it may not be as always as in this example. 72 00:04:13,650 --> 00:04:20,160 So if the function uses several pointers and performs a pointer assignment, you may not immediately 73 00:04:20,160 --> 00:04:24,690 notice that you have returned a pointer to a stack allocated object. 74 00:04:24,690 --> 00:04:31,860 So the best course of action is simply to not return raw pointers from functions. 75 00:04:32,220 --> 00:04:39,270 But if you do want to use this style of programming, make sure that the memory buffer is passed in 76 00:04:39,930 --> 00:04:44,370 through the parameters so the function does not own the buffer. 77 00:04:44,370 --> 00:04:52,890 So when the memory buffer is passed through the parameter or is dynamically allocated and you are passing 78 00:04:52,890 --> 00:04:55,230 ownership to the color. 79 00:04:55,230 --> 00:04:59,720 So in with with this method, it's more safer than this here. 80 00:04:59,730 --> 00:05:02,960 So this leads on to another issue. 81 00:05:03,120 --> 00:05:09,990 So if you call delete on a pointer and then later in your code, try to access the pointer, you will 82 00:05:09,990 --> 00:05:14,820 be accessing memory that is potentially being used by other variables. 83 00:05:15,030 --> 00:05:25,080 So to alleviate this problem, you can get into the habit of assigning a pointer to null pointer here. 84 00:05:25,260 --> 00:05:29,790 So when you delete it and check for the null before using assignment. 85 00:05:29,790 --> 00:05:32,880 So it's more a good practice here. 86 00:05:32,880 --> 00:05:39,780 So alternatively you can use smart pointer object which will do this for you here, but smart pointers 87 00:05:39,780 --> 00:05:42,030 will be covered in next lecture. 88 00:05:42,030 --> 00:05:42,540 So here.