Skip to content

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

Merged
merged 16 commits into from
Jun 14, 2025

Conversation

sofiadanaile
Copy link
Contributor

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 #6868

Type of Change

  • Adding a new entry

Checklist

  • [✅] All writings are my own.
  • [✅] My entry follows the Codecademy Docs style guide.
  • [✅] My changes generate no new warnings.
  • [✅] I have performed a self-review of my own writing and code.
  • [✅] I have checked my entry and corrected any misspellings.
  • [✅] I have made corresponding changes to the documentation if needed.
  • [✅] I have confirmed my changes are not being pushed from my forked main branch.
  • [✅] I have confirmed that I'm pushing from a new branch named after the changes I'm making.
  • [✅] I have linked any issues that are relevant to this PR in the Issues Solved section.

@CLAassistant
Copy link

CLAassistant commented May 27, 2025

CLA assistant check
All committers have signed the CLA.

@sofiadanaile
Copy link
Contributor Author

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.
I created this entry by reading official documentation and consulting with a knowledgeable classmate from university who actually works with C++.
I linked the Codecademy C++ course as a useful resource for anyone wanting to learn more, since I saw a Pointers section in the course syllabus.
I’m happy to receive any feedback or suggestions to improve this entry.

Thanks!🌸🙏

@sofiadanaile
Copy link
Contributor Author

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❤️

@mamtawardhani mamtawardhani self-assigned this May 28, 2025
@mamtawardhani mamtawardhani added c++ C++ entries new entry New entry or entries status: under review Issue or PR is currently being reviewed labels May 28, 2025
Copy link
Collaborator

@mamtawardhani mamtawardhani left a 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! 😃

Comment on lines 2 to 3
title: .reset()
description: Releases ownership of the managed object and optionally takes ownership of a new object.
Copy link
Collaborator

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'

Comment on lines 6 to 10
### .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.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
### .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.

Comment on lines 17 to 18
smart_pointer.reset(); // Releases ownership of the current object
smart_pointer.reset(new Type()); // Deletes current object and takes ownership of the new one
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
```cpp
```pseudo

Copy link
Collaborator

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

Comment on lines 20 to 21

---
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
---

return 0;
}
```

Copy link
Collaborator

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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
## Codebyte
## Codebyte Example

Comment on lines 46 to 47

---
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
---

Comment on lines 73 to 78
## 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)


Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
## 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)

Copy link
Collaborator

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

Comment on lines 54 to 68
#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;
}
Copy link
Collaborator

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

@sofiadanaile
Copy link
Contributor Author

Hi! 🌸
Sorry for requesting a re-review before pushing my latest changes — I only realized a few minutes later that I hadn’t pushed them yet, so I just pushed them ASAP.
Thank you so much for your feedback! Please let me know if there’s anything else I should update.

I’m happy to make any further changes if needed! <3

Copy link
Collaborator

@mamtawardhani mamtawardhani left a 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! 🚀

@Sriparno08 Sriparno08 added the status: under review Issue or PR is currently being reviewed label Jun 4, 2025
Copy link
Collaborator

@Sriparno08 Sriparno08 left a 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!

@Sriparno08 Sriparno08 merged commit db5dfa5 into Codecademy:main Jun 14, 2025
7 checks passed
Copy link

👋 @sofiadanaile
You have contributed to Codecademy Docs, and we would like to know more about you and your experience.
Please take a minute to fill out this four question survey to help us better understand Docs contributions and how we can improve the experience for you and our learners.
Thank you for your help!

🎉 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.
If you're appearing as anonymous and want to be credited, visit the linked accounts page and ensure that your GitHub account is linked.

@Sriparno08 Sriparno08 added status: review 2️⃣ completed and removed status: under review Issue or PR is currently being reviewed labels Jun 14, 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.

[Term Entry] C++ - Pointers: .reset()
4 participants