You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cpp/raw-pointers.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
---
2
2
title: "Raw pointers (C++)"
3
3
description: "How to use raw pointers in C++"
4
-
ms.date: "11/19/2019"
4
+
ms.date: "04/21/2020"
5
5
helpviewer_keywords: ["pointers [C++]"]
6
6
---
7
7
# Raw pointers (C++)
8
8
9
-
A pointer is a type of variable that stores the address of an object in memory and is used to access that object. A *raw pointer* is a pointer whose lifetime is not controlled by an encapsulating object such as a [smart pointer](smart-pointers-modern-cpp.md). A raw pointer can be assigned the address of another non-pointer variable, or it can be assigned a value of [nullptr](nullptr.md). A pointer that has not been assigned a value contains random data.
9
+
A *pointer* is a type of variable that stores the address of an object in memory, and is used to access that object. A *raw pointer* is a pointer whose lifetime is not controlled by an encapsulating object, such as a [smart pointer](smart-pointers-modern-cpp.md). A raw pointer can be assigned the address of another non-pointer variable, or it can be assigned a value of [nullptr](nullptr.md). A pointer that hasn't been assigned a value contains random data.
10
10
11
11
A pointer can also be *dereferenced* to retrieve the value of the object that it points at. The *member access operator* provides access to an object's members.
12
12
@@ -18,15 +18,15 @@ A pointer can also be *dereferenced* to retrieve the value of the object that it
18
18
int j = *p; // dereference p to retrieve the value at its address
19
19
```
20
20
21
-
A pointer can point to a typed object or to **void**. When a program allocates a new object on the [heap](https://wikipedia.org/wiki/Heap) in memory, it receives the address of that object in the form of a pointer. Such pointers are called *owning pointers*; an owning pointer (or a copy of it) must be used to explicitly delete the heap-allocated object when it is no longer needed. Failure to delete the memory results in a *memory leak* and renders that memory location unavailable to any other program on the machine. For more information, see [new and delete operators](new-and-delete-operators.md).
21
+
A pointer can point to a typed object or to **void**. When a program allocates a new object on the [heap](https://wikipedia.org/wiki/Heap) in memory, it receives the address of that object in the form of a pointer. Such pointers are called *owning pointers*. An owning pointer (or a copy of it) must be used to explicitly delete the heap-allocated object when it's no longer needed. Failure to delete the memory results in a *memory leak*, and renders that memory location unavailable to any other program on the machine. For more information, see [new and delete operators](new-and-delete-operators.md).
22
22
23
23
```cpp
24
24
MyClass* mc = new MyClass(); // allocate object on the heap
A pointer (if it isn't declared as **const**) can be incremented or decremented so that it points to a new location in memory. This is called *pointer arithmetic* and is used in C-style programming to iterate over elements in arrays or other data structures. A **const** pointer can't be made to point to a different memory location, and in that sense is very similar to a [reference](references-cpp.md). For more information, see [const and volatile pointers](const-and-volatile-pointers.md).
29
+
A pointer (if it isn't declared as **const**) can be incremented or decremented to point at a new location in memory. This operation is called *pointer arithmetic*, and it's used in C-style programming to iterate over elements in arrays or other data structures. A **const** pointer can't be made to point to a different memory location, and in that sense is similar to a [reference](references-cpp.md). For more information, see [const and volatile pointers](const-and-volatile-pointers.md).
30
30
31
31
```cpp
32
32
// declare a C-style string. Compiler adds terminating '\0'.
@@ -40,13 +40,13 @@ A pointer (if it isn't declared as **const**) can be incremented or decremented
40
40
// pconst2 = &c2; // Error! pconst2 is const.
41
41
```
42
42
43
-
On 64-bit operating systems, a pointer has a size of 64 bits; a system's pointer size determines how much addressable memory it can have. All copies of a pointer point to the same memory location. Pointers (along with references) are used extensively in C++ to pass larger objects to and from functionsbecause it is usually far more efficient to copy an object's 64-bit address than to copy an entire object. When defining a function, specify pointer parameters as **const** unless you intend for the function to modify the object. In general, **const** references are the preferred way to pass objects to functions unless the value of the object can possibly be **nullptr**.
43
+
On 64-bit operating systems, a pointer has a size of 64 bits. A system's pointer size determines how much addressable memory it can have. All copies of a pointer point to the same memory location. Pointers (along with references) are used extensively in C++ to pass larger objects to and from functions. That's because it's often more efficient to copy an object's address than to copy the entire object. When defining a function, specify pointer parameters as **const** unless you intend the function to modify the object. In general, **const** references are the preferred way to pass objects to functions unless the value of the object can possibly be **nullptr**.
44
44
45
45
[Pointers to functions](#pointers_to_functions) enable functions to be passed to other functions and are used for "callbacks" in C-style programming. Modern C++ uses [lambda expressions](lambda-expressions-in-cpp.md) for this purpose.
46
46
47
47
## Initialization and member access
48
48
49
-
The following example shows how to declarea raw pointer and initialize it with an object allocated on the heap, and then how to use it. It also shows a few of the dangers associated with raw pointers. (Remember, this is C-style programming and not modern C++!)
49
+
The following example shows how to declare, initialize, and use a raw pointer. It's initialized with an object allocated on the heap, which you must explicitly delete. The example also shows a few of the dangers associated with raw pointers. (Remember, this example is C-style programming and not modern C++!)
50
50
51
51
```cpp
52
52
#include<iostream>
@@ -126,7 +126,7 @@ int main()
126
126
127
127
## Pointer arithmetic and arrays
128
128
129
-
Pointers and arrays are closely related. When an array is passed by-value to a function, it is passed as a pointer to the first element. The following example demonstrates the following important properties of pointers and arrays:
129
+
Pointers and arrays are closely related. When an array is passed by-value to a function, it's passed as a pointer to the first element. The following example demonstrates the following important properties of pointers and arrays:
130
130
131
131
- the `sizeof` operator returns the total size in bytes of an array
132
132
- to determine the number of elements, divide total bytes by the size of one element
@@ -157,7 +157,7 @@ int main()
157
157
}
158
158
```
159
159
160
-
Certain arithmetic operations can be performed on non-const pointers to make them point to a new memory location. A pointer can be incremented and decremented using the **++**, **+=**, **-=** and **--** operators. This technique can be used in arrays and is especially useful in buffers of untyped data. A **void\***increments by the size of a **char** (1 byte). A typed pointer increments by size of the type it points to.
160
+
Certain arithmetic operations can be used on non-const pointers to make them point to a new memory location. Pointers are incremented and decremented using the **++**, **+=**, **-=** and **--** operators. This technique can be used in arrays and is especially useful in buffers of untyped data. A **void\***gets incremented by the size of a **char** (1 byte). A typed pointer gets incremented by size of the type it points to.
161
161
162
162
The following example demonstrates how pointer arithmetic can be used to access individual pixels in a bitmap on Windows. Note the use of **new** and **delete**, and the dereference operator.
163
163
@@ -226,9 +226,9 @@ int main()
226
226
227
227
## void* pointers
228
228
229
-
A pointer to **void** simply points to a raw memory location. Sometimes it is necessary to use **void\*** pointers, for example when passing between C++ code and C functions.
229
+
A pointer to **void** simply points to a raw memory location. Sometimes it's necessary to use **void\*** pointers, for example when passing between C++ code and C functions.
230
230
231
-
When a typed pointer is cast to a void pointer, the contents of the memory location are not changed, but the type information is lost, so that you can't perform increment or decrement operations. A memory location can be cast, for example, from MyClass* to void* and back again to MyClass*. Such operations are inherently error-prone and require great care to avoid errors. Modern C++ discourages the use of void pointers unless absolutely necessary.
231
+
When a typed pointer is cast to a void pointer, the contents of the memory location are unchanged. However, the type information is lost, so that you can't do increment or decrement operations. A memory location can be cast, for example, from `MyClass*` to `void*` and back again to `MyClass*`. Such operations are inherently error-prone and require great care to avoid errors. Modern C++ discourages the use of void pointers in almost all circumstances.
232
232
233
233
```cpp
234
234
@@ -284,7 +284,7 @@ int main()
284
284
285
285
## <a name="pointers_to_functions"></a> Pointers to functions
286
286
287
-
In C-style programming, function pointers are used primarily to pass functions to other functions. In this scenario, the caller can customize the behavior of a function without modifying it. In modern C++, [lambda expressions](lambda-expressions-in-cpp.md) provide the same capability with greater type safety and other advantages.
287
+
In C-style programming, function pointers are used primarily to pass functions to other functions. It allows the caller to customize the behavior of a function without modifying it. In modern C++, [lambda expressions](lambda-expressions-in-cpp.md) provide the same capability with greater type safety and other advantages.
288
288
289
289
A function pointer declaration specifies the signature that the pointed-to function must have:
0 commit comments