Skip to content

Commit dd69283

Browse files
author
Colin Robertson
committed
Fix co-kr issue 987 bad raw pointer example code
1 parent 1f2c29d commit dd69283

1 file changed

Lines changed: 2 additions & 4 deletions

File tree

‎docs/cpp/raw-pointers.md‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@ A pointer can also be *dereferenced* to retrieve the value of the object that it
1616
int i = 5;
1717
p = &i; // assign pointer to address of object
1818
int j = *p; // dereference p to retrieve the value at its address
19-
2019
```
2120

2221
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).
2322

2423
```cpp
25-
2624
MyClass* mc = new MyClass(); // allocate object on the heap
2725
mc->print(); // access class member
2826
delete mc; // delete object (please don't forget!)
@@ -112,13 +110,13 @@ int main()
112110
pmc2->print(); // "Erika, 108"
113111

114112
// Pass the pointer to a function.
115-
func_A(mc);
113+
func_A(pmc);
116114
pmc->print(); // "Erika, 3"
117115
pmc2->print(); // "Erika, 3"
118116

119117
// Dereference the pointer and pass a copy
120118
// of the pointed-to object to a function
121-
func_B(*mc);
119+
func_B(*pmc);
122120
pmc->print(); // "Erika, 3" (original not modified by function)
123121

124122
delete(pmc); // don't forget to give memory back to operating system!

0 commit comments

Comments
 (0)