Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions content/cpp/concepts/pointers/terms/reset/reset.md
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.
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'

---

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


---

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

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

```

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


## Example

Copy link
Collaborator

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?

```cpp
#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;
}
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
#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;
}

Copy link
Collaborator

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

```

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

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.

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


Here’s a compilable example using `.reset()` with a `std::shared_ptr`:

```codebyte/cpp
#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

```

---

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