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