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
@@ -10,8 +10,21 @@ A pointer stores the address of a object in memory and is used to access that ob
10
10
11
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
12
13
+
deferencing
14
+
15
+
char* p ="Hello";
16
+
17
+
print p vs. *p
18
+
13
19
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
20
21
+
## Syntax
22
+
23
+
```cpp
24
+
MyClass* p;
25
+
26
+
```
27
+
15
28
## Initialization and member access
16
29
17
30
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.
@@ -94,12 +107,75 @@ A const pointer can point to either a const or non-const object, but can't be mo
94
107
95
108
## Pointer arithmetic
96
109
110
+
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.
97
111
112
+
```cpp
113
+
// pointers.cpp : This file contains the 'main' function. Program execution begins and ends there.
114
+
//
115
+
116
+
// Pointer arithmetic may be done with an array declared
117
+
// on the stack or allocated on the heap with new.
118
+
// The increment operator takes into account the size
119
+
// of the objects pointed to.
120
+
p = new int[5];
121
+
for (i = 0; i < 5; i++, p++) {
122
+
*p = i * 10;
123
+
printf_s("0x%p %d\n", p, *p);
124
+
}
125
+
126
+
// A common expression seen is dereferencing in combination
127
+
// with increment or decrement operators, as shown here.
128
+
// The indirection operator * takes precedence over the
129
+
// increment operator ++.
130
+
// These are particularly useful in manipulating char arrays.
131
+
char s1[4] = "cat";
132
+
char s2[4] = "dog";
133
+
char* p1 = s1;
134
+
char* p2 = s2;
135
+
136
+
// the following is a string copy operation
137
+
while (*p1++ = *p2++);
138
+
139
+
// s2 was copied into s1, so now they are both equal to "dog"
140
+
printf_s("%s %s", s1, s2);
141
+
142
+
#include <iostream>
143
+
#include <string>
144
+
145
+
using namespace std;
146
+
147
+
class MyClass
148
+
{
149
+
public:
150
+
int num;
151
+
std::string name;
152
+
void print() { std::cout << name << ":" << num << std::endl; }
153
+
};
154
+
155
+
int main()
156
+
{
157
+
MyClass arr[3];
158
+
arr[0] = {108, "Mike"};
159
+
arr[1] = {109, "Lisa"};
160
+
arr[2] = {110, "Tabby"};
161
+
162
+
MyClass* pmc = &arr[0];
163
+
cout << pmc->name;
164
+
pmc++;
165
+
cout << pmc->name;
166
+
pmc++;
167
+
cout << pmc->name;
168
+
}
169
+
170
+
```
98
171
99
172
## void* pointers
100
173
174
+
A pointer to **void** simply points to a raw memory location. Sometimes it is unfortunately necessary to cast a typed pointer to **void\***, for example when passing a pointer to a C++ class object to a C function. The contents of the memory location are not changed, but the type information is lost, so that you cannot perform increment or decrement operations. A memory location can be cast from MyClass* to void* and back again to MyClass* although such operations are inherently error-prone. Modern C++ discourages the use of void pointers unless absolutely necessary.
0 commit comments