Skip to content

Commit a1d1fed

Browse files
author
mikeblome
committed
updates per peer review
1 parent f3b72b7 commit a1d1fed

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

docs/cpp/raw-pointers.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@ ms.date: "11/12/2019"
44
helpviewer_keywords: ["pointers [C++]"]
55
---
66

7-
# Raw Pointers (C++)
7+
# Raw pointers (C++)
88

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

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

1913
```cpp
2014
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
2317
p = &i; // assign pointer to address of object
2418
int j = *p; // dereference p to retrieve the value at its address
2519

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+
2626
MyClass* mc = new MyClass(); // allocate object on the heap
2727
mc->print(); // access class member
2828
delete mc; // delete object (please don't forget!)
29+
```
30+
31+
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).
2932

33+
```cpp
3034
// declare a C-style string. Compiler adds terminating '\0'.
3135
const char* str = "Hello world";
3236

@@ -38,14 +42,15 @@ Pointers to functions enable functions to be passed to other functions and are u
3842
// pconst2 = &c2; // Error! pconst2 is const.
3943
```
4044

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+
4149
## Initialization and member access
4250

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

4553
```cpp
46-
// pointers.cpp : This file contains the 'main' function. Program execution begins and ends there.
47-
//
48-
4954
#include <iostream>
5055
#include <string>
5156

@@ -278,7 +283,7 @@ int main()
278283
}
279284
```
280285
281-
## Pointers to functions
286+
## <a name="pointers_to_functions"></a> Pointers to functions
282287
283288
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.
284289

0 commit comments

Comments
 (0)