-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Add .reset() term for smart pointers in C++ #6913
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
Changes from 2 commits
d30a62b
462d25a
2f11b9e
4027526
def2f94
c58b492
a461ff4
324e6f5
5db28a8
8335d26
1ee8f12
c22bf59
9172800
4ae9d84
6d024ea
3d53dad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -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. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ### .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. | |
| 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. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ```cpp | |
| ```pseudo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
syntax is added in pseudo block
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| smart_pointer.reset(); // Releases ownership of the current object | |
| smart_pointer.reset(new Type()); // Deletes current object and takes ownership of the new one | |
| ptr.reset(); // Releases ownership and deletes the managed object | |
| ptr.reset(new_ptr); // Replaces the managed object with a new one |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| --- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add a one line description of the example code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| #include <iostream> | |
| #include <memory> | |
| int main() { | |
| std::unique_ptr<int> 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; | |
| } | |
| #include <iostream> | |
| #include <memory> | |
| int main() { | |
| std::unique_ptr<int> 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; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation needs to be two spaces only
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you also add the output block wrapped in the shell block
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| --- |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ## Codebyte | |
| ## Codebyte Example |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keep the indentation to two spaces only
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ## 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
References are not required, and can be deleted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you rewrite this in the following format:
Title: '.reset()'
Description: 'Releases ownership of the managed object and optionally takes ownership of a new object.'
Subjects:
Tags:
CatalogContent: