Skip to content

Commit 90eb0d4

Browse files
author
Colin Robertson
committed
Acrolinx all the things
1 parent dd69283 commit 90eb0d4

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

docs/cpp/raw-pointers.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
22
title: "Raw pointers (C++)"
33
description: "How to use raw pointers in C++"
4-
ms.date: "11/19/2019"
4+
ms.date: "04/21/2020"
55
helpviewer_keywords: ["pointers [C++]"]
66
---
77
# Raw pointers (C++)
88

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

1111
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.
1212

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

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

2323
```cpp
2424
MyClass* mc = new MyClass(); // allocate object on the heap
2525
mc->print(); // access class member
2626
delete mc; // delete object (please don't forget!)
2727
```
2828

29-
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).
3030

3131
```cpp
3232
// 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
4040
// pconst2 = &c2; // Error! pconst2 is const.
4141
```
4242

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 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**.
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**.
4444

4545
[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.
4646

4747
## Initialization and member access
4848

49-
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++!)
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++!)
5050

5151
```cpp
5252
#include <iostream>
@@ -126,7 +126,7 @@ int main()
126126
127127
## Pointer arithmetic and arrays
128128
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:
130130
131131
- the `sizeof` operator returns the total size in bytes of an array
132132
- to determine the number of elements, divide total bytes by the size of one element
@@ -157,7 +157,7 @@ int main()
157157
}
158158
```
159159

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

162162
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.
163163

@@ -226,9 +226,9 @@ int main()
226226

227227
## void* pointers
228228

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

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

233233
```cpp
234234

@@ -284,7 +284,7 @@ int main()
284284
285285
## <a name="pointers_to_functions"></a> Pointers to functions
286286
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.
288288
289289
A function pointer declaration specifies the signature that the pointed-to function must have:
290290

0 commit comments

Comments
 (0)