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
+8-7Lines changed: 8 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,10 +3,11 @@ title: "Raw pointers (C++)"
3
3
description: "How to use raw pointers in C++"
4
4
ms.date: "04/21/2020"
5
5
helpviewer_keywords: ["pointers [C++]"]
6
+
no-loc: [void, nullptr, const, char, new, delete]
6
7
---
7
8
# Raw pointers (C++)
8
9
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
+
A *pointer* is a type of variable. It 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
11
11
12
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
13
@@ -18,15 +19,15 @@ A pointer can also be *dereferenced* to retrieve the value of the object that it
18
19
int j = *p; // dereference p to retrieve the value at its address
19
20
```
20
21
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
+
A pointer can point to a typed object or to **void**. When a program allocates an 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 free the heap-allocated object when it's no longer needed. Failure to free the memory results in a *memory leak*, and renders that memory location unavailable to any other program on the machine. Memory allocated using **new** must be freed by using **delete** (or **delete\[]**). For more information, see [new and delete operators](new-and-delete-operators.md).
22
23
23
24
```cpp
24
25
MyClass* mc = new MyClass(); // allocate object on the heap
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
+
A pointer (if it isn't declared as **const**) can be incremented or decremented to point at another location in memory. This operation is called *pointer arithmetic*. 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
31
31
32
```cpp
32
33
// declare a C-style string. Compiler adds terminating '\0'.
@@ -46,7 +47,7 @@ On 64-bit operating systems, a pointer has a size of 64 bits. A system's pointer
46
47
47
48
## Initialization and member access
48
49
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
+
The following example shows how to declare, initialize, and use a raw pointer. It's initialized using **new** to point 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
51
51
52
```cpp
52
53
#include<iostream>
@@ -157,7 +158,7 @@ int main()
157
158
}
158
159
```
159
160
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
+
Certain arithmetic operations can be used on non-const pointers to make them point to another 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
162
162
163
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
164
@@ -284,7 +285,7 @@ int main()
284
285
285
286
## <a name="pointers_to_functions"></a> Pointers to functions
286
287
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
+
In C-style programming, function pointers are used primarily to pass functions to other functions. This technique 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
289
289
290
A function pointer declaration specifies the signature that the pointed-to function must have:
290
291
@@ -302,7 +303,7 @@ void (*x)();
302
303
int (*i)(int i, string s, double d);
303
304
```
304
305
305
-
The following example shows a function `combine` that takes as a parameter any function that accepts a `std::string` and returns a `std::string`. Depending on the function that is passed to `combine` it will either prepend or append a string.
306
+
The following example shows a function `combine` that takes as a parameter any function that accepts a `std::string` and returns a `std::string`. Depending on the function that's passed to `combine`, it either prepends or appends a string.
0 commit comments