From d30a62be1b7c75ed4c2961b3fb257d79fad2fc25 Mon Sep 17 00:00:00 2001 From: Sofia Danaile Date: Tue, 27 May 2025 23:20:08 +0300 Subject: [PATCH 1/4] Add .reset() term for smart pointers in C++ --- .../concepts/pointers/terms/reset/reset.md | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 content/cpp/concepts/pointers/terms/reset/reset.md diff --git a/content/cpp/concepts/pointers/terms/reset/reset.md b/content/cpp/concepts/pointers/terms/reset/reset.md new file mode 100644 index 00000000000..451376d0c2f --- /dev/null +++ b/content/cpp/concepts/pointers/terms/reset/reset.md @@ -0,0 +1,78 @@ +--- +title: .reset() +description: Releases ownership of the managed object and optionally takes ownership of a new object. +--- + +### .reset() + +The `.reset()` method is used with **smart pointers** in C++ (such as `std::unique_ptr` and `std::shared_ptr`). It releases ownership of the currently managed object and, optionally, replaces it with a new one. + +This method helps prevent memory leaks by ensuring the previously managed object is properly deleted when it's no longer needed. + +--- + +## Syntax + +```cpp +smart_pointer.reset(); // Releases ownership of the current object +smart_pointer.reset(new Type()); // Deletes current object and takes ownership of the new one +``` + +--- + +## Example + +```cpp +#include +#include + +int main() { + std::unique_ptr ptr(new int(42)); + std::cout << "Value before reset: " << *ptr << std::endl; + + ptr.reset(); // Releases ownership and deletes the managed object + + if (!ptr) { + std::cout << "Pointer is null after reset." << std::endl; + } + + return 0; +} +``` + +In this example, a `std::unique_ptr` manages an `int` with a value of 42. +After calling `.reset()`, the pointer releases ownership of the object and becomes null. +The check `if (!ptr)` confirms the pointer was successfully reset. + +--- + +## Codebyte + +Here’s a compilable example using `.reset()` with a `std::shared_ptr`: + +```codebyte/cpp +#include +#include + +int main() { + std::shared_ptr message = std::make_shared("Hello from Codecademy!"); + std::cout << "Message: " << *message << std::endl; + + message.reset(); // Releases ownership of the managed string + + if (!message) { + std::cout << "The pointer has been reset." << std::endl; + } + + return 0; +} +``` + +--- + +## References + +- [`std::unique_ptr::reset()` - cppreference.com](https://en.cppreference.com/w/cpp/memory/unique_ptr/reset) +- [Codecademy C++ Course](https://www.codecademy.com/learn/learn-c-plus-plus) + + From 2f11b9ecd3d4fafcfca857dd8f2e8756c344d41c Mon Sep 17 00:00:00 2001 From: Sofia Danaile Date: Thu, 29 May 2025 07:31:45 +0300 Subject: [PATCH 2/4] Enhance .reset() documentation: added example description, included output block, and improved formatting as per review feedback --- .../concepts/pointers/terms/reset/reset.md | 86 +++++++++---------- 1 file changed, 40 insertions(+), 46 deletions(-) diff --git a/content/cpp/concepts/pointers/terms/reset/reset.md b/content/cpp/concepts/pointers/terms/reset/reset.md index 451376d0c2f..14c47b59c53 100644 --- a/content/cpp/concepts/pointers/terms/reset/reset.md +++ b/content/cpp/concepts/pointers/terms/reset/reset.md @@ -1,78 +1,72 @@ ---- -title: .reset() -description: Releases ownership of the managed object and optionally takes ownership of a new object. ---- - -### .reset() - -The `.reset()` method is used with **smart pointers** in C++ (such as `std::unique_ptr` and `std::shared_ptr`). It releases ownership of the currently managed object and, optionally, replaces it with a new one. - -This method helps prevent memory leaks by ensuring the previously managed object is properly deleted when it's no longer needed. +Title: '.reset()' +Description: 'Releases ownership of the managed object and optionally takes ownership of a new object.' +Subjects: +'Code Foundations' +'Computer Science' +Tags: +'Containers' +'Pointers' +CatalogContent: +'learn-c++' +'paths/computer-science' + +The **`.reset()`** method is used with smart pointers in C++ (such as `std::unique_ptr` and `std::shared_ptr`). It releases ownership of the currently managed object and optionally takes ownership of a new one. + +This method safely manages dynamic memory by deleting the previously managed object (if any), thereby helping to prevent memory leaks. --- ## Syntax -```cpp -smart_pointer.reset(); // Releases ownership of the current object -smart_pointer.reset(new Type()); // Deletes current object and takes ownership of the new one +```pseudo +ptr.reset(); // Releases ownership and deletes the managed object +ptr.reset(new_ptr); // Replaces the managed object with a new one ``` ---- - ## Example +This example demonstrates how `.reset()` releases ownership of the managed object, safely deleting it and setting the pointer to null. + ```cpp #include #include int main() { - std::unique_ptr ptr(new int(42)); - std::cout << "Value before reset: " << *ptr << std::endl; + std::unique_ptr ptr(new int(42)); + std::cout << "Value before reset: " << *ptr << std::endl; - ptr.reset(); // Releases ownership and deletes the managed object + ptr.reset(); // Releases ownership and deletes the managed object - if (!ptr) { - std::cout << "Pointer is null after reset." << std::endl; - } - - return 0; + if (!ptr) { + std::cout << "Pointer is null after reset." << std::endl; + } + return 0; } ``` +```shell +Value before reset: 42 +Pointer is null after reset. +``` + In this example, a `std::unique_ptr` manages an `int` with a value of 42. After calling `.reset()`, the pointer releases ownership of the object and becomes null. The check `if (!ptr)` confirms the pointer was successfully reset. ---- - -## Codebyte +## Codebyte Example Here’s a compilable example using `.reset()` with a `std::shared_ptr`: ```codebyte/cpp #include #include - int main() { - std::shared_ptr message = std::make_shared("Hello from Codecademy!"); - std::cout << "Message: " << *message << std::endl; - - message.reset(); // Releases ownership of the managed string - - if (!message) { - std::cout << "The pointer has been reset." << std::endl; - } - - return 0; + std::unique_ptr ptr(new int(42)); + std::cout << "Value before reset: " << *ptr << std::endl; + ptr.reset(); // Releases ownership and deletes the managed object + if (!ptr) { + std::cout << "Pointer is null after reset." << std::endl; + } + return 0; } ``` - ---- - -## References - -- [`std::unique_ptr::reset()` - cppreference.com](https://en.cppreference.com/w/cpp/memory/unique_ptr/reset) -- [Codecademy C++ Course](https://www.codecademy.com/learn/learn-c-plus-plus) - - From c22bf5982d69df9514b95e1dbfa541b7f072d105 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 4 Jun 2025 15:16:02 +0530 Subject: [PATCH 3/4] fixes in content and changed codebyte as it was similar to example --- .../concepts/pointers/terms/reset/reset.md | 81 +++++++++++++------ 1 file changed, 55 insertions(+), 26 deletions(-) diff --git a/content/cpp/concepts/pointers/terms/reset/reset.md b/content/cpp/concepts/pointers/terms/reset/reset.md index 14c47b59c53..b1f06d897b3 100644 --- a/content/cpp/concepts/pointers/terms/reset/reset.md +++ b/content/cpp/concepts/pointers/terms/reset/reset.md @@ -1,31 +1,31 @@ +--- Title: '.reset()' Description: 'Releases ownership of the managed object and optionally takes ownership of a new object.' Subjects: -'Code Foundations' -'Computer Science' +- 'Code Foundations' +- 'Computer Science' Tags: -'Containers' -'Pointers' +- 'Containers' +- 'Pointers' CatalogContent: -'learn-c++' -'paths/computer-science' +- 'learn-c++' +- 'paths/computer-science' +--- -The **`.reset()`** method is used with smart pointers in C++ (such as `std::unique_ptr` and `std::shared_ptr`). It releases ownership of the currently managed object and optionally takes ownership of a new one. +The **`.reset()`** method is used with smart pointers in C++ (such as `std::unique_ptr` and `std::shared_ptr`). It releases ownership of the currently managed object (deleting it if this is the last owner) and optionally takes ownership of a new object passed as a raw pointer. This method safely manages dynamic memory by deleting the previously managed object (if any), thereby helping to prevent memory leaks. ---- - ## Syntax ```pseudo -ptr.reset(); // Releases ownership and deletes the managed object -ptr.reset(new_ptr); // Replaces the managed object with a new one +ptr.reset(); // Releases ownership and deletes the managed object +ptr.reset(raw_ptr); // Releases current object and takes ownership of raw_ptr ``` ## Example -This example demonstrates how `.reset()` releases ownership of the managed object, safely deleting it and setting the pointer to null. +This example demonstrates how `.reset()` releases ownership of the managed object, deletes it, and sets the pointer to null: ```cpp #include @@ -44,29 +44,58 @@ int main() { } ``` -```shell -Value before reset: 42 -Pointer is null after reset. +The output of this code is: + +```shell +Value before reset: 42 +Pointer is null after reset. ``` -In this example, a `std::unique_ptr` manages an `int` with a value of 42. -After calling `.reset()`, the pointer releases ownership of the object and becomes null. -The check `if (!ptr)` confirms the pointer was successfully reset. +In this example: + +- A `std::unique_ptr` manages an `int` with a value of 42. +- After calling `.reset()`, the managed object is deleted and the pointer becomes null. +- The check `if (!ptr)` confirms the pointer was successfully reset. ## Codebyte Example -Here’s a compilable example using `.reset()` with a `std::shared_ptr`: +Run the following example to understand how the `.reset()` works: ```codebyte/cpp #include #include +#include + +class TV { +public: + TV(std::string brand) : brand_(brand) { + std::cout << brand_ << " TV is turned ON.\n"; + } + ~TV() { + std::cout << brand_ << " TV is turned OFF.\n"; + } + void watch() const { + std::cout << "Watching " << brand_ << " TV.\n"; + } +private: + std::string brand_; +}; + int main() { - std::unique_ptr ptr(new int(42)); - std::cout << "Value before reset: " << *ptr << std::endl; - ptr.reset(); // Releases ownership and deletes the managed object - if (!ptr) { - std::cout << "Pointer is null after reset." << std::endl; - } - return 0; + std::unique_ptr remote(new TV("Samsung")); // Remote controls Samsung TV + remote->watch(); + + // Replace old TV with a new LG TV + remote.reset(new TV("LG")); // Old TV turned off, now controlling LG TV + remote->watch(); + + // Put down the remote, no TV controlled now + remote.reset(); // LG TV turned off, remote controls nothing + + if (!remote) { + std::cout << "Remote controls no TV now.\n"; + } + + return 0; } ``` From 3d53dad277368d7ce9681863d5c900c6d1f0a574 Mon Sep 17 00:00:00 2001 From: Sriparno Roy Date: Sat, 14 Jun 2025 20:19:55 +0530 Subject: [PATCH 4/4] Minor changes --- .../concepts/pointers/terms/reset/reset.md | 67 ++++++++++--------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/content/cpp/concepts/pointers/terms/reset/reset.md b/content/cpp/concepts/pointers/terms/reset/reset.md index b1f06d897b3..b8631a46e07 100644 --- a/content/cpp/concepts/pointers/terms/reset/reset.md +++ b/content/cpp/concepts/pointers/terms/reset/reset.md @@ -1,15 +1,15 @@ --- -Title: '.reset()' -Description: 'Releases ownership of the managed object and optionally takes ownership of a new object.' -Subjects: -- 'Code Foundations' -- 'Computer Science' -Tags: -- 'Containers' -- 'Pointers' -CatalogContent: -- 'learn-c++' -- 'paths/computer-science' +Title: '.reset()' +Description: 'Releases ownership of the managed object and optionally takes ownership of a new object.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Containers' + - 'Pointers' +CatalogContent: + - 'learn-c++' + - 'paths/computer-science' --- The **`.reset()`** method is used with smart pointers in C++ (such as `std::unique_ptr` and `std::shared_ptr`). It releases ownership of the currently managed object (deleting it if this is the last owner) and optionally takes ownership of a new object passed as a raw pointer. @@ -59,7 +59,7 @@ In this example: ## Codebyte Example -Run the following example to understand how the `.reset()` works: +Run the following example to understand how the `.reset()` method works: ```codebyte/cpp #include @@ -68,34 +68,35 @@ Run the following example to understand how the `.reset()` works: class TV { public: - TV(std::string brand) : brand_(brand) { - std::cout << brand_ << " TV is turned ON.\n"; - } - ~TV() { - std::cout << brand_ << " TV is turned OFF.\n"; - } - void watch() const { - std::cout << "Watching " << brand_ << " TV.\n"; - } + TV(std::string brand) : brand_(brand) { + std::cout << brand_ << " TV is turned ON.\n"; + } + ~TV() { + std::cout << brand_ << " TV is turned OFF.\n"; + } + void watch() const { + std::cout << "Watching " << brand_ << " TV.\n"; + } + private: - std::string brand_; + std::string brand_; }; int main() { - std::unique_ptr remote(new TV("Samsung")); // Remote controls Samsung TV - remote->watch(); + std::unique_ptr remote(new TV("Samsung")); // Remote controls Samsung TV + remote->watch(); - // Replace old TV with a new LG TV - remote.reset(new TV("LG")); // Old TV turned off, now controlling LG TV - remote->watch(); + // Replace old TV with a new LG TV + remote.reset(new TV("LG")); // Old TV turned off, now controlling LG TV + remote->watch(); - // Put down the remote, no TV controlled now - remote.reset(); // LG TV turned off, remote controls nothing + // Put down the remote, no TV controlled now + remote.reset(); // LG TV turned off, remote controls nothing - if (!remote) { - std::cout << "Remote controls no TV now.\n"; - } + if (!remote) { + std::cout << "Remote controls no TV now.\n"; + } - return 0; + return 0; } ```