1 00:00:00,860 --> 00:00:05,690 C plus plus allows you to have direct access to memory throughout pointers. 2 00:00:05,780 --> 00:00:11,030 So this gives you a lot of flexibility and potentially it allows you to improve the performance of your 3 00:00:11,030 --> 00:00:14,900 code by eliminating some unnecessary copying of data. 4 00:00:15,530 --> 00:00:23,600 So however, it has provides an extra source of errors so some some can be fatal for your applications 5 00:00:23,600 --> 00:00:31,340 or even worse, because poor use of a memory buffers can open security holes in your code that can allow 6 00:00:31,370 --> 00:00:33,490 malware to take over the machine. 7 00:00:33,500 --> 00:00:38,170 So clearly pointers are important aspect of a C plus plus. 8 00:00:38,180 --> 00:00:45,200 So in this lecture you will see how to declare a pointers and initialize them to memory locations. 9 00:00:45,500 --> 00:00:51,950 How to allocate memory on the stack C plus plus restore, and how to use C plus plus arrays. 10 00:00:52,070 --> 00:00:56,240 So C plus plus uses the same syntax. 11 00:00:57,070 --> 00:01:06,760 As see here because in C and C plus plus to declare pointers are the same and assign them to a memory. 12 00:01:06,760 --> 00:01:09,790 And it has like like pointers, arithmetic. 13 00:01:09,790 --> 00:01:15,570 So like c c plus plus allows you to allocate memory on the stack. 14 00:01:15,580 --> 00:01:20,620 So there is an automatic memory cleanup when the stack frame is destroyed. 15 00:01:20,620 --> 00:01:27,340 So and the dynamic allocation on the C plus plus free store where the programmer has a responsibility 16 00:01:27,340 --> 00:01:29,680 to release memory. 17 00:01:29,800 --> 00:01:32,830 So this section will be covered. 18 00:01:32,830 --> 00:01:38,200 These aspects here so so the syntax access memory in C plus plus. 19 00:01:39,360 --> 00:01:40,380 Is straightforward. 20 00:01:40,390 --> 00:01:42,790 So the this operator. 21 00:01:43,580 --> 00:01:48,100 The and operator returns the address of an object. 22 00:01:48,110 --> 00:01:55,010 So the object can be a variable, a boolean type, or the instance of a custom type or even a function. 23 00:01:55,800 --> 00:01:59,190 A which function pointers will be covered in next lectures. 24 00:01:59,190 --> 00:02:05,040 So the address is assigned to a tight pointer or a void pointer here. 25 00:02:05,070 --> 00:02:06,360 Void pointer. 26 00:02:07,170 --> 00:02:14,010 So the void pointer should be treated as merely storage for the memory address because you cannot access 27 00:02:14,010 --> 00:02:17,550 data and you cannot perform pointer arithmetic. 28 00:02:18,270 --> 00:02:22,290 This means that manipulate the pointer values using arithmetic operators. 29 00:02:22,290 --> 00:02:27,590 So you can't do an on these actions on a void pointer. 30 00:02:27,600 --> 00:02:36,000 So the pointer variables are usually declared using a type of this ternary symbol. 31 00:02:36,570 --> 00:02:43,170 So for example, let's declare a pointer here, integer A or let's first let's declare it just an usual 32 00:02:43,170 --> 00:02:53,910 integer, for example, 92 and then integer P here this ternary operator and operator here, here. 33 00:02:53,910 --> 00:02:59,500 So in this code, the variable A actually the variable A is an integer. 34 00:03:00,150 --> 00:03:05,970 And the compiler and linker will determine where this variable will be located at. 35 00:03:06,000 --> 00:03:15,390 So usually a variable in a function will be on a stack frame as described as we will describe in later 36 00:03:15,390 --> 00:03:16,050 section. 37 00:03:16,050 --> 00:03:19,920 So actually, let me open our diagram here. 38 00:03:22,020 --> 00:03:22,770 Okay. 39 00:03:23,770 --> 00:03:24,400 Here. 40 00:03:24,730 --> 00:03:25,720 So. 41 00:03:27,590 --> 00:03:28,640 At runtime. 42 00:03:30,750 --> 00:03:33,230 Uh, this stack will be created. 43 00:03:36,100 --> 00:03:40,720 Essentially a chunk of memory will be allocated and the space will be. 44 00:03:41,490 --> 00:03:45,360 Reversed reserved here in the stack memory for the variable. 45 00:03:45,840 --> 00:03:51,630 So then the program puts the variable 92in that memory. 46 00:03:52,050 --> 00:04:01,590 Next, the address of the memory allocated for the variable A is placed in the variable P here. 47 00:04:02,380 --> 00:04:09,790 The memory usage in this code is illustrated the memory usage of this code here. 48 00:04:10,520 --> 00:04:16,400 So the pointer holds a value of eight. 49 00:04:16,430 --> 00:04:18,670 See here this memory address. 50 00:04:18,680 --> 00:04:23,080 Notice that the lowest byte is stored in the lowest byte in memory. 51 00:04:23,090 --> 00:04:29,000 So this is, for example, this code illustrates the 32 bit machine. 52 00:04:29,000 --> 00:04:37,100 So the memory location here has a value of two A and six zero. 53 00:04:38,560 --> 00:04:42,250 And that is a value of 92. 54 00:04:42,280 --> 00:04:48,550 The value of variable A here signs P is also variable. 55 00:04:48,550 --> 00:04:56,380 It also occupies space in the memory, and in this case the compiler has put the pointer lower in memory 56 00:04:56,380 --> 00:04:58,030 than the data it points to. 57 00:04:58,030 --> 00:05:04,270 And in this case the two variables are not contiguous. 58 00:05:04,270 --> 00:05:10,270 So with the variable allocated on the stack like this, you should make no assumptions about where in 59 00:05:10,270 --> 00:05:15,910 memory the variables are allocated so nor their location in relation to the other variables. 60 00:05:15,910 --> 00:05:20,290 So this could assume a 32 bit operating system. 61 00:05:20,290 --> 00:05:22,240 And so the pointer. 62 00:05:23,040 --> 00:05:30,030 A P occupies 32 bits and contains 32 bit address. 63 00:05:30,060 --> 00:05:35,550 If the operating system is 64 bits, then the pointer will be 64 bits wide. 64 00:05:35,550 --> 00:05:39,180 But the integer may still be in 3032 bits. 65 00:05:39,240 --> 00:05:40,460 Just one note here. 66 00:05:40,470 --> 00:05:49,560 So in this lecture, in this course, we will use 32 bit pointers for the simple convenience that 32 67 00:05:49,590 --> 00:05:53,460 bit addresses take less typing than 64 bit addresses. 68 00:05:53,940 --> 00:06:00,630 So the typed pointer is declared with the this symbol here. 69 00:06:01,650 --> 00:06:09,690 So and we will refer to this as an integer pointer here because the pointer points to memory that holds 70 00:06:09,720 --> 00:06:12,190 an integer when declaring a pointer. 71 00:06:12,210 --> 00:06:20,280 The convention is to put the this ternary operator next to the variable name rather than next to the 72 00:06:20,280 --> 00:06:20,780 type. 73 00:06:20,790 --> 00:06:26,970 So the syntax emphasizes that the type pointer to is an integer. 74 00:06:26,970 --> 00:06:34,500 So however it is important to use this syntax if you declare more than one variable in a single statement 75 00:06:34,500 --> 00:06:35,130 here. 76 00:06:35,220 --> 00:06:42,510 So actually, let's let me show this in a real practicing here. 77 00:06:45,030 --> 00:06:45,690 Here. 78 00:06:49,780 --> 00:06:50,590 And let me. 79 00:06:52,060 --> 00:06:53,930 Close this diagram. 80 00:06:53,950 --> 00:06:54,550 Okay. 81 00:06:55,330 --> 00:07:05,860 So however here, it's important, as I said earlier, to use the syntax if you declare more than one 82 00:07:05,860 --> 00:07:06,610 pointer. 83 00:07:06,610 --> 00:07:13,300 So, for example, we can declare BP here and. 84 00:07:14,100 --> 00:07:14,370 Oops. 85 00:07:16,120 --> 00:07:18,070 Here we can declare the. 86 00:07:21,220 --> 00:07:23,860 B, P and. 87 00:07:25,850 --> 00:07:27,170 See here. 88 00:07:28,230 --> 00:07:28,650 Um. 89 00:07:30,360 --> 00:07:36,570 Here, we declared more than one variable in a single statement. 90 00:07:37,450 --> 00:07:44,260 It's clear that the first variable is an integer pointer and the second is an integer. 91 00:07:45,500 --> 00:07:49,290 But the following year is not so clear. 92 00:07:49,310 --> 00:07:50,600 As you can see here. 93 00:07:51,530 --> 00:07:58,340 You might interrupt this to mean that the type of both variables is an integer, but this is not the 94 00:07:58,340 --> 00:08:02,390 case here as this declares a pointer and integer. 95 00:08:02,390 --> 00:08:05,120 So this is the pointer and this is the integer. 96 00:08:05,120 --> 00:08:10,850 So if you want to declare two pointers, then apply this on each variable. 97 00:08:10,850 --> 00:08:15,230 So I want to say that this doesn't matter where it goes. 98 00:08:15,260 --> 00:08:20,390 As I said earlier, in C spaces, in most cases cplusplus doesn't matter. 99 00:08:20,390 --> 00:08:23,630 So it's it's because the visual. 100 00:08:24,670 --> 00:08:26,080 No problems here. 101 00:08:26,470 --> 00:08:36,760 So it's not it you if you put this ternary operator here, it will be nice and more explanatory for 102 00:08:36,760 --> 00:08:37,890 your code. 103 00:08:37,900 --> 00:08:48,060 So it's probably better to just declare two pointers in separate lines in this case here, CP here CPI. 104 00:08:48,960 --> 00:08:57,660 So when you apply the size of operator to a pointer here, you will get the size of the pointer, not 105 00:08:57,660 --> 00:08:58,570 what it points to. 106 00:08:58,590 --> 00:09:12,390 So this on 32 bit machine here, size of here, size of a size of this integer pointer will return. 107 00:09:12,390 --> 00:09:17,040 Will return a return. 108 00:09:18,890 --> 00:09:23,300 Will return for in 32 bit machine. 109 00:09:24,730 --> 00:09:26,060 Will return. 110 00:09:26,080 --> 00:09:28,390 Eight return. 111 00:09:29,050 --> 00:09:29,770 Eight. 112 00:09:29,950 --> 00:09:32,410 In 64. 113 00:09:32,440 --> 00:09:34,660 Bit machine here. 114 00:09:35,290 --> 00:09:36,310 So. 115 00:09:37,500 --> 00:09:45,630 This is an important observation, especially when we discuss the C plus plus built in arrays in later 116 00:09:45,630 --> 00:09:46,440 lectures. 117 00:09:47,410 --> 00:09:58,120 So to access the data pointed to by a pointer, you must reference it using the using this operator 118 00:09:58,120 --> 00:09:58,630 here. 119 00:09:59,760 --> 00:10:00,540 So. 120 00:10:01,710 --> 00:10:03,770 Now let's write an example code here. 121 00:10:03,780 --> 00:10:05,190 As you can see, we have. 122 00:10:06,810 --> 00:10:10,650 Let's actually let's delete these quotes because we don't need it anymore. 123 00:10:12,130 --> 00:10:14,230 And add this here. 124 00:10:16,460 --> 00:10:25,550 So let's write an integer here and we'll take the pointer. 125 00:10:25,600 --> 00:10:30,680 P So use like this on the right hand side of an assignment. 126 00:10:30,710 --> 00:10:36,200 The the reference pointer gives access to the value pointed to by the pointer. 127 00:10:36,200 --> 00:10:41,620 So g here is initialized to 92. 128 00:10:41,630 --> 00:10:49,280 So compare this to the declaration of pointer where the this symbol is also used but has a different 129 00:10:49,280 --> 00:10:50,060 meaning. 130 00:10:50,120 --> 00:10:57,590 So the dereference operator does more than give a read access to the data at the memory location as 131 00:10:57,590 --> 00:11:03,650 long as the pointer does not restrict it using the const keyword which you will learn about later. 132 00:11:03,650 --> 00:11:09,500 So you can dereference the pointer to write to a memory location to for example. 133 00:11:10,070 --> 00:11:10,270 Um. 134 00:11:10,580 --> 00:11:11,270 Okay. 135 00:11:11,900 --> 00:11:13,070 C out. 136 00:11:14,140 --> 00:11:16,720 See out here a. 137 00:11:17,980 --> 00:11:19,770 Or we have to use steady. 138 00:11:20,970 --> 00:11:23,130 See out a. 139 00:11:24,690 --> 00:11:25,080 Here. 140 00:11:28,470 --> 00:11:29,090 Listed. 141 00:11:31,090 --> 00:11:33,430 And then. 142 00:11:35,000 --> 00:11:39,820 Now let's declare this point here and change this. 143 00:11:39,830 --> 00:11:42,110 Change this variable. 144 00:11:42,880 --> 00:11:43,690 To. 145 00:11:44,700 --> 00:11:45,120 A. 146 00:11:47,490 --> 00:11:53,460 And then integer or Yeah P equals 99. 147 00:11:55,090 --> 00:11:55,750 Here. 148 00:11:57,970 --> 00:12:01,510 Or let's, for example, make it 600. 149 00:12:04,620 --> 00:12:06,000 And see out. 150 00:12:06,510 --> 00:12:07,620 See out. 151 00:12:08,960 --> 00:12:20,660 A we have to STD out a here and end line STD and line. 152 00:12:22,420 --> 00:12:25,540 So let's run this code here. 153 00:12:26,450 --> 00:12:27,050 Yeah. 154 00:12:27,260 --> 00:12:28,100 So. 155 00:12:29,400 --> 00:12:31,560 In this code the pointer. 156 00:12:32,920 --> 00:12:33,310 P. 157 00:12:34,400 --> 00:12:40,370 Points to the location in a memory of the variable named A here. 158 00:12:40,460 --> 00:12:48,510 So in this case, using this braces syntax here actually let's make spaces to make it more clear. 159 00:12:48,530 --> 00:12:55,460 So assigning the the references pointers assigns the value to the location that the pointer points to. 160 00:12:55,490 --> 00:13:07,790 So the result is that on the last line here, the variable A will have a value of 600, not 92.