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