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
+19-14Lines changed: 19 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,17 +4,11 @@ ms.date: "11/12/2019"
4
4
helpviewer_keywords: ["pointers [C++]"]
5
5
---
6
6
7
-
# Raw Pointers (C++)
7
+
# Raw pointers (C++)
8
8
9
-
A pointer stores the address of an object in memory and is used to access that object. 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. 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).
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.
10
10
11
-
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).
12
-
13
-
Pointers (along with references) are used extensively in C++ to pass larger objects to and from functions because it is usually far more efficient to copy an object's 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**.
14
-
15
-
Pointers to functions enable functions to be passed to other functions and are used for "callbacks" in C-style programming.
16
-
17
-
## Syntax
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.
18
12
19
13
```cpp
20
14
int* p = nullptr; // declare pointer and initialize it
@@ -23,10 +17,20 @@ Pointers to functions enable functions to be passed to other functions and are u
23
17
p = &i; // assign pointer to address of object
24
18
int j = *p; // dereference p to retrieve the value at its address
25
19
20
+
```
21
+
22
+
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).
23
+
24
+
```cpp
25
+
26
26
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
32
33
+
```cpp
30
34
// declare a C-style string. Compiler adds terminating '\0'.
31
35
constchar* str = "Hello world";
32
36
@@ -38,14 +42,15 @@ Pointers to functions enable functions to be passed to other functions and are u
38
42
// pconst2 = &c2; // Error! pconst2 is const.
39
43
```
40
44
45
+
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 because 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**.
46
+
47
+
[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.
48
+
41
49
## Initialization and member access
42
50
43
-
The following example shows how to declare a 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.
51
+
The following example shows how to declare a 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++!)
44
52
45
53
```cpp
46
-
// pointers.cpp : This file contains the 'main' function. Program execution begins and ends there.
47
-
//
48
-
49
54
#include<iostream>
50
55
#include<string>
51
56
@@ -278,7 +283,7 @@ int main()
278
283
}
279
284
```
280
285
281
-
## Pointers to functions
286
+
## <a name="pointers_to_functions"></a> Pointers to functions
282
287
283
288
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.
0 commit comments