-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Conversation
Hi I’m new to C++ and haven’t worked with it before. I chose this topic as my first entry because, unfortunately, other topics that felt closer to me were already claimed, so I picked one that seemed more approachable. Thanks!🌸🙏 |
Earlier, when my contribution was being reviewed, I was asked: “Is this your first contribution to Codecademy Docs? If so, we’re curious to know how you found out about contributing to Docs.” I found out about contributing to Docs only thanks to your excellent iOS Career Path course, where I came across this assignment. I’m incredibly grateful for this amazing course and the wealth of knowledge I’m gaining through it. Codecademy team, you are the best! A huge thank you to the entire team from a sincere beginner❤️ |
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.
Hey @sofiadanaile, thank you for contributing to Codecademy Docs, the entry is nicely written! 😄
I've suggested a few changes, could you please review and modify those at your earliest convenience? Thank you! 😃
title: .reset() | ||
description: Releases ownership of the managed object and optionally takes ownership of a new object. |
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:
- 'Code Foundations'
- 'Computer Science'
Tags: - 'Containers'
- 'Pointers'
CatalogContent: - 'learn-c++'
- 'paths/computer-science'
### .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. |
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.
### .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. |
smart_pointer.reset(); // Releases ownership of the current object | ||
smart_pointer.reset(new Type()); // Deletes current object and takes ownership of the new one |
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 |
|
||
## Syntax | ||
|
||
```cpp |
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
|
||
--- |
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.
--- |
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.
can you also add the output block wrapped in the shell
block
|
||
--- | ||
|
||
## Codebyte |
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.
--- |
## 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 | |
- [`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
#include <iostream> | ||
#include <memory> | ||
|
||
int main() { | ||
std::shared_ptr<std::string> message = std::make_shared<std::string>("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; | ||
} |
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
…utput block, and improved formatting as per review feedback
…into term-reset-cpp
Hi! 🌸 I’m happy to make any further changes if needed! <3 |
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.
thank you @sofiadanaile for the changes 😄
The entry looks good for a second round of review! 🚀
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.
The entry looks good to be merged, @sofiadanaile!
👋 @sofiadanaile 🎉 Your contribution(s) can be seen here: https://www.codecademy.com/resources/docs/cpp/pointers/reset Please note it may take a little while for changes to become visible. |
Description
Created a new entry for the .reset() C++ smart pointers (std::unique_ptr and std::shared_ptr). Explained the purpose, syntax, and provided examples.Issue Solved
Closes #6868Type of Change
Checklist
main
branch.Issues Solved
section.