Skip to content

Commit 1674949

Browse files
author
Colin Robertson
committed
tweak no-loc values
1 parent 90eb0d4 commit 1674949

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

docs/cpp/raw-pointers.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ title: "Raw pointers (C++)"
33
description: "How to use raw pointers in C++"
44
ms.date: "04/21/2020"
55
helpviewer_keywords: ["pointers [C++]"]
6+
no-loc: [void, nullptr, const, char, new, delete]
67
---
78
# Raw pointers (C++)
89

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.
1011

1112
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.
1213

@@ -18,15 +19,15 @@ A pointer can also be *dereferenced* to retrieve the value of the object that it
1819
int j = *p; // dereference p to retrieve the value at its address
1920
```
2021

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).
2223

2324
```cpp
2425
MyClass* mc = new MyClass(); // allocate object on the heap
2526
mc->print(); // access class member
2627
delete mc; // delete object (please don't forget!)
2728
```
2829

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+
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).
3031

3132
```cpp
3233
// 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
4647

4748
## Initialization and member access
4849

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++!)
5051

5152
```cpp
5253
#include <iostream>
@@ -157,7 +158,7 @@ int main()
157158
}
158159
```
159160

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.
161162

162163
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.
163164

@@ -284,7 +285,7 @@ int main()
284285
285286
## <a name="pointers_to_functions"></a> Pointers to functions
286287
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.
288289
289290
A function pointer declaration specifies the signature that the pointed-to function must have:
290291
@@ -302,7 +303,7 @@ void (*x)();
302303
int (*i)(int i, string s, double d);
303304
```
304305

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.
306307

307308
```cpp
308309
#include <iostream>

0 commit comments

Comments
 (0)