From e86f793296a089e32253019d0bffdd2b479beb48 Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Wed, 23 Jul 2025 18:11:44 +0800 Subject: [PATCH 1/5] Fix wrong sentence that implies `delete[]` can be used in place of `delete` in "Raw pointers (C++)" --- docs/cpp/raw-pointers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/cpp/raw-pointers.md b/docs/cpp/raw-pointers.md index c12c7051c6..babfbd1ea0 100644 --- a/docs/cpp/raw-pointers.md +++ b/docs/cpp/raw-pointers.md @@ -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). ```cpp MyClass* mc = new MyClass(); // allocate object on the heap From e720bf9884cf492a38334363027f4fff1736cbac Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Wed, 23 Jul 2025 18:21:59 +0800 Subject: [PATCH 2/5] Remove sentence that implies pointer arithmetic on `void*` is allowed in "Raw pointers (C++)" --- docs/cpp/raw-pointers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/cpp/raw-pointers.md b/docs/cpp/raw-pointers.md index babfbd1ea0..f6d8d4aed4 100644 --- a/docs/cpp/raw-pointers.md +++ b/docs/cpp/raw-pointers.md @@ -156,7 +156,7 @@ 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. +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 typed pointer gets incremented by size of the type it points to. The following example demonstrates how pointer arithmetic can be used to access individual pixels in a bitmap on Windows. Note the use of **`new`** and **`delete`**, and the dereference operator. From 66d88ddfb744e93e3c406c0ce9bcb23ab508f98c Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Wed, 23 Jul 2025 18:31:29 +0800 Subject: [PATCH 3/5] Add 2 `Output` code blocks in "Raw pointers (C++)" --- docs/cpp/raw-pointers.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/cpp/raw-pointers.md b/docs/cpp/raw-pointers.md index f6d8d4aed4..5b00104653 100644 --- a/docs/cpp/raw-pointers.md +++ b/docs/cpp/raw-pointers.md @@ -156,6 +156,10 @@ int main() } ``` +```Output +1 2 3 4 5 +``` + 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 typed pointer gets incremented by size of the type it points to. The following example demonstrates how pointer arithmetic can be used to access individual pixels in a bitmap on Windows. Note the use of **`new`** and **`delete`**, and the dereference operator. @@ -332,6 +336,11 @@ int main() } ``` +```Output +hello world from MSVC +Good morning and hello world from MSVC +``` + ## See also [Smart pointers](smart-pointers-modern-cpp.md)\ From e0c3d3fd6cf87ca3bc5393f4d259cffa931b9046 Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Wed, 23 Jul 2025 18:34:23 +0800 Subject: [PATCH 4/5] Use idiomatic usage of `delete` in "Raw pointers (C++)" --- docs/cpp/raw-pointers.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/cpp/raw-pointers.md b/docs/cpp/raw-pointers.md index 5b00104653..743e7f3038 100644 --- a/docs/cpp/raw-pointers.md +++ b/docs/cpp/raw-pointers.md @@ -119,8 +119,8 @@ int main() func_B(*pmc); pmc->print(); // "Erika, 3" (original not modified by function) - delete(pmc); // don't forget to give memory back to operating system! - // delete(pmc2); //crash! memory location was already deleted + delete pmc; // don't forget to give memory back to operating system! + // delete pmc2; //crash! memory location was already deleted } ``` @@ -268,7 +268,7 @@ int main() void* p = static_cast(mc); MyClass* mc2 = static_cast(p); std::cout << mc2->name << std::endl; // "Marian" - delete(mc); + delete mc; // use operator new to allocate untyped memory block void* pvoid = operator new(1000); From c3195c9abd56b46f616126b7ffdf54d4ddba8e40 Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Wed, 23 Jul 2025 18:38:42 +0800 Subject: [PATCH 5/5] Fix incorrect delimiter in output comments for example in "Raw pointers (C++)" --- docs/cpp/raw-pointers.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/cpp/raw-pointers.md b/docs/cpp/raw-pointers.md index 743e7f3038..3c4d07c50e 100644 --- a/docs/cpp/raw-pointers.md +++ b/docs/cpp/raw-pointers.md @@ -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" } int main() @@ -99,25 +99,25 @@ int main() MyClass* pcopy = &mc; // Use the -> operator to access the object's public members - pmc->print(); // "Nick, 108" + pmc->print(); // "Nick:108" // Copy the pointer. Now pmc and pmc2 point to same object! MyClass* pmc2 = pmc; // Use copied pointer to modify the original object pmc2->name = "Erika"; - pmc->print(); // "Erika, 108" - pmc2->print(); // "Erika, 108" + pmc->print(); // "Erika:108" + pmc2->print(); // "Erika:108" // Pass the pointer to a function. func_A(pmc); - pmc->print(); // "Erika, 3" - pmc2->print(); // "Erika, 3" + pmc->print(); // "Erika:3" + pmc2->print(); // "Erika:3" // Dereference the pointer and pass a copy // of the pointed-to object to a function func_B(*pmc); - pmc->print(); // "Erika, 3" (original not modified by function) + pmc->print(); // "Erika:3" (original not modified by function) delete pmc; // don't forget to give memory back to operating system! // delete pmc2; //crash! memory location was already deleted