Skip to content

Tweak "Raw pointers (C++)" topic #5574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Rageking8
Copy link
Contributor

Fix a bunch of mistakes and perform some improvements. See review comments for more information.

Copy link
Contributor

@Rageking8 : Thanks for your contribution! The author(s) and reviewer(s) have been notified to review your proposed change.

Copy link
Contributor

Learn Build status updates of commit c3195c9:

✅ Validation status: passed

File Status Preview URL Details
docs/cpp/raw-pointers.md ✅Succeeded

For more details, please refer to the build report.

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

A pointer can point to a typed object or to **`void`**. When a program allocates an 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 free the heap-allocated object when it's no longer needed. Failure to free the memory results in a *memory leak*, and renders that memory location unavailable to any other program on the machine. Memory allocated using **`new`** must be freed by using **`delete`** (or **`delete[]`**). For more information, see [`new` and `delete` operators](new-and-delete-operators.md).
A pointer can point to a typed object or to **`void`**. When a program allocates an 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 free the heap-allocated object when it's no longer needed. Failure to free the memory results in a *memory leak*, and renders that memory location unavailable to any other program on the machine. Memory allocated with **`new`** must be freed using **`delete`**, and memory allocated with **`new[]`** must be freed using **`delete[]`**. For more information, see [`new` and `delete` operators](new-and-delete-operators.md).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Memory allocated using new must be freed by using delete (or delete[]).

This implies delete[] can be used in place of delete, which is wrong. Hence, this sentence is extended to make it explicit that new must be matched with delete, and new[] must be matched with delete[].

@@ -78,7 +78,7 @@ void func_B(MyClass mc)
// This statement modifies only the local copy of mc.
mc.num = 21;
std::cout << "Local copy of mc:";
mc.print(); // "Erika, 21"
mc.print(); // "Erika:21"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output comments used the wrong delimiter (", " instead of the correct ":"). All subsequent occurrences are fixed.

Comment on lines +122 to +123
delete pmc; // don't forget to give memory back to operating system!
// delete pmc2; //crash! memory location was already deleted
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer the more idiomatic delete expr; over delete(expr);, even though the latter technically works. As an aside, found this interesting comment at c++ - Is there any difference between delete x and delete(x)? - Stack Overflow.

@@ -156,7 +156,11 @@ int main()
}
```

Certain arithmetic operations can be used on non-`const` pointers to make them point to another 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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A void* gets incremented by the size of a char (1 byte).

This implies that pointer arithmetic can be done on void*, but that causes a hard error, since it is non-standard and only available as an extension on GCC (not sure if this extension exists for MSVC). Hence, this sentence is removed, but we can consider explicitly stating that it is not allowed.

@@ -156,7 +156,11 @@ int main()
}
```

Certain arithmetic operations can be used on non-`const` pointers to make them point to another 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.
```Output
1 2 3 4 5
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a trailing space here as that's the verbatim output of this snippet.

@ttorble
Copy link
Contributor

ttorble commented Jul 23, 2025

@TylerMSFT

Can you review the proposed changes?

IMPORTANT: When the changes are ready for publication, adding a #sign-off comment is the best way to signal that the PR is ready for the review team to merge.

#label:"aq-pr-triaged"
@MicrosoftDocs/public-repo-pr-review-team

@prmerger-automator prmerger-automator bot added the aq-pr-triaged Tracking label for the PR review team label Jul 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants