Skip to content

Commit c3362dc

Browse files
author
mikeblome
committed
updating pointers topic for human consumption
1 parent bc595b1 commit c3362dc

File tree

2 files changed

+99
-6
lines changed

2 files changed

+99
-6
lines changed

docs/cpp/pointers-cpp.md

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,105 @@
11
---
22
title: "Pointers (C++)"
3-
ms.date: "11/04/2016"
3+
ms.date: "11/06/2019"
44
helpviewer_keywords: ["declarators, pointers", "declarations, pointers", "pointers [C++]", "pointers, declarations"]
55
ms.assetid: 595387c5-8e58-4670-848f-344c7caf985e
66
---
77
# Pointers (C++)
88

9-
Pointers are declared using the following sequence.
9+
A pointer stores the address of a 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**. A non-const pointer can be incremented or decremented, which causes it to point 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 cannot be made to point to a different memory location, and in that sense is very similar to a [reference](references-cpp.md).
10+
11+
Pointers are used extensively in C++ to pass larger objects to and from functions because it is far more efficient to copy a 64-bit value than to copy an entire object. When a program allocates a new object on the [heap]() in memory, it receives the address of that object in the form of a pointer. Such pointers are called *owning pointers* because they must be used to explicitly delete the object when it is no longer needed. Pointers to functions enable functions to be passed to other functions and are used for "callbacks" in C-style programming.
12+
13+
In C and C++, pointer errors are by far the greatest cause of crashes, hangs, data corruption, security holes and general programmer misery. In modern C++, the use of *raw pointers* is strongly discouraged except in very specific scenarios. Modern C++ provides *smart pointers* for allocating objects, *iterators* for traversing data structures, and *lambda expressions* for passing callable functions. By using these language and library facilities instead of raw pointers, you will make your program safer, easier to debug, and simpler to understand and maintain.
14+
15+
## Initialization and member access
16+
17+
The following example shows how to declare a 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 pointers.
18+
19+
```cpp
20+
// pointers.cpp : This file contains the 'main' function. Program execution begins and ends there.
21+
//
22+
23+
#include <iostream>
24+
#include <string>
25+
26+
class MyClass
27+
{
28+
public:
29+
int num;
30+
std::string name;
31+
void print() { std::cout << name << ":" << num << std::endl; }
32+
};
33+
34+
// Accepts a MyClass pointer
35+
void func_A(MyClass* mc)
36+
{
37+
// Modify the object that mc points to.
38+
// All copies of the pointer will point to
39+
// the same modified object.
40+
mc->num = 3;
41+
}
42+
43+
// Accepts a MyClass object
44+
void func_B(MyClass mc)
45+
{
46+
// mc here is a regular object, not a pointer.
47+
// Use the "." operator to access members.
48+
// This statement modifies only the local copy of mc.
49+
mc.num = 21;
50+
std::cout << "Local copy of mc:";
51+
mc.print(); // "Lisa, 21"
52+
}
53+
54+
55+
int main()
56+
{
57+
// Use the * operator to declare a pointer type
58+
// Use new to allocate and initialize memory
59+
MyClass* mc = new MyClass{ 108, "Mike" };
60+
61+
// Prints the memory address. Usually not what you want.
62+
// std:: cout << mc << std::endl;
63+
64+
// Use the -> operator to access the object's public members
65+
mc->print(); // "Mike, 108"
66+
67+
// Copy the pointer. Now mc and mc2 point to same object!
68+
MyClass* mc2 = mc;
69+
70+
// Use copied pointer to modify the original object
71+
mc2->name = "Lisa";
72+
mc->print(); // "Lisa, 108"
73+
mc2->print(); // "Lisa, 108"
74+
75+
// Pass the pointer to a function.
76+
func_A(mc);
77+
mc->print(); // "Lisa, 3"
78+
mc2->print(); // "Lisa, 3"
79+
80+
// Dereference the pointer and pass a copy
81+
// of the pointed-to object to a function
82+
func_B(*mc);
83+
mc->print(); // "Lisa, 3" (original not modified by function)
84+
85+
delete(mc); // don't forget to give memory back to operating system!
86+
// delete(mc2); //crash! memory location was already deleted
87+
}
88+
89+
```
90+
91+
## Const pointers and pointers to const objects
92+
93+
A const pointer can point to either a const or non-const object, but can't be modified to point to a new object. When a non-const or const pointer points to a const object, the pointer cannot be used to modify the object and can only call const member functions. For more information, see [const and volatile pointers](const-and-volatile-pointers.md).
94+
95+
## Pointer arithmetic
96+
97+
98+
99+
## void* pointers
100+
101+
## Pointers to functions
102+
10103
11104
> \[*storage-class-specifiers*] \[*cv-qualifiers*] *type-specifiers* \[*ms-modifier*] *declarator* **;**
12105

docs/cpp/void-cpp.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ ms.assetid: d203edba-38e6-4056-8b89-011437351057
77
---
88
# void (C++)
99

10-
When used as a function return type, the **void** keyword specifies that the function does not return a value. When used for a function's parameter list, void specifies that the function takes no parameters. When used in the declaration of a pointer, void specifies that the pointer is "universal."
10+
When used as a function return type, the **void** keyword specifies that the function does not return a value. When used for a function's parameter list, **void** specifies that the function takes no parameters. When used in the declaration of a pointer, **void** specifies that the pointer is "universal."
1111

12-
If a pointer's type is `void *`, the pointer can point to any variable that is not declared with the **const** or **volatile** keyword. A void pointer cannot be dereferenced unless it is cast to another type. A void pointer can be converted into any other type of data pointer.
12+
If a pointer's type is **void\***, the pointer can point to any variable that is not declared with the **const** or **volatile** keyword. A **void\*** pointer cannot be dereferenced unless it is cast to another type. A **void\*** pointer can be converted into any other type of data pointer.
1313

14-
A void pointer can point to a function, but not to a class member in C++.
14+
A **void** pointer can point to a function, but not to a class member in C++.
1515

16-
You cannot declare a variable of type void.
16+
You cannot declare a variable of type **void**.
1717

1818
## Example
1919

0 commit comments

Comments
 (0)