课程: Complete Guide to C++ Programming Foundations
Pointers
- [Instructor] Pointers are an essential part of the C programming language, which is the foundation of C++. In this course, we will only cover the basics of pointers. Pointers are a special type of variables that hold memory addresses. For example, for 32 bit architectures, pointers are 32 bits wide. Pointers can point to any data type, meaning that the data stored at the memory address can be an integer, a float, an object, or even another pointer. This versatility is one of the reasons pointers are so powerful in C++. When working with pointers, you might encounter two important concepts, NULL and nullptr. NULL is a legacy constant used to indicate that a pointer doesn't point to any valid memory address. However, in modern C++, nullptr is preferred because it provides type safety and avoids potential ambiguities. The use of nullptr is often recommended because it is a constant of its own type reserved for this purpose, whereas NULL is just the integer value zero. It's a simple change that makes a significant difference in pointer management. Lastly, remember that when you want to access or modify the data a pointer is pointing to, you need to de-reference the pointer. This means using the de-reference operator asterisk to access the actual data stored at the memory address the pointer holds, but remember to avoid de-referencing a pointer that points to NULL or nullptr as it leads to undefined behavior and can cause your program to crash. Let's see an example. Suppose we have this partial memory map with the locations shown at the left and the slots are the memory units. Now, the memory is organized with an address for each byte and I'll show you an example with 32-bit integers. That's why addresses are shown in steps of four because each integer takes four bytes. So when you declare a 32-bit integer like this, int a = 37, the compiler reserves a memory location for it. Let's say, a got address 104 assigned, since we initialized it at 37, the contents of that memory address will be 37. Now to declare a pointer, the syntax goes like this. We specify the type of the variable we will be pointing to, followed by the name of new pointer, preceded by an asterisk. In this example, that would be int *ptr. Remember that pointers are variables themselves and so the pointer is allocated at some memory location. In this example, let's say it goes to address 120. Now it gets interesting, if we assign an address to the pointer, then it will point to that address. So let's make ptr point to a. You can do it like this, ptr = &a. This prefix, &, is the address of operator and it does precisely that, return the address of the variable at its right. If we run this line of code, the contents of address 120 will be the address of a or 104. Because of this, it is colloquially said that ptr now, quote unquote, points to a. So let's see the same example of integer a and pointer ptr in a live demo. Here we have some startup code that isn't really doing much with the pointer, so I will go line by line filling in the blanks. The final version of the code is supposed to print what the strings are saying. So in line nine, we have a declared and initialized at 37, and in line 10 we have the declaration of ptr as a pointer to an int. Then in line 12, we are assigning nullptr to ptr. Once again, nullptr is a constant of its own type that represents a null location in memory. It is used to indicate that a pointer does not point to any valid memory location. The rest of the code assumes that ptr points to a. So starting at line 14, we are using ptr as a placeholder to print a series of messages with the following information: the value of a, the address ptr is pointing to, the address of a, the content of the memory location where ptr is pointing, and the address of ptr. So let's start fixing the code at line 12, assigning the address of a to ptr. That's &a. There, now ptr points to a. Now let's fill in the blanks. First, we'll show the value of a in line 14. For this, we simply use the variable's name, a. Next, we'll show the address where ptr is pointing to. Remember, this is the content of the variable ptr, which is an address, so I'll simply use the name of this variable again, that's ptr. Then in line 16, we'll show the address of a. For this, we use the address of operator we used before, that's &A. Then in line 17, we will print the contents of the memory address where ptr is pointing. To access this data, we use the indirection operator, that's a prefix asterisk. Notice that cout will print an integer for this last value, not a string or a floating point number or anything other than an int. That's because we declared ptr as a pointer to int. That's one of the reasons to specify a type for the target of a pointer. And lastly, in line 18, we print the address of ptr as a variable. Again, I'll use the address of operator, &. Now, be aware that for the addresses, we will not see 104 and 120 as in the example, instead, we will see whichever addresses are assigned to the variables at runtime. Let's see these results. We have that the value of a is 37. Ptr is pointing to an address ending in dd3c. The address of a is the same as where ptr is pointing, that's good. Where ptr is pointing, we have 37, that's also good. And lastly, the address of ptr is different from the address of a. That's it for the essentials of pointers. Remember, once you have fully understood the use of pointers, you may want to look into smart pointers for a safer alternative.
内容
-
-
-
-
-
-
(已锁定)
Arrays7 分钟 27 秒
-
Pointers7 分钟 59 秒
-
(已锁定)
How arrays and pointers are related5 分钟 14 秒
-
(已锁定)
Using objects with pointers10 分钟 56 秒
-
(已锁定)
The vector class5 分钟
-
(已锁定)
Using objects with vectors6 分钟 29 秒
-
(已锁定)
References4 分钟 56 秒
-
(已锁定)
C strings11 分钟 16 秒
-
(已锁定)
The string class4 分钟 14 秒
-
(已锁定)
Solution: Vector manipulation2 分钟
-
(已锁定)
-
-
-
-
-
-