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
Merged
Changes from all 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
102 changes: 102 additions & 0 deletions content/cpp/concepts/pointers/terms/reset/reset.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
---
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.

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(raw_ptr); // Releases current object and takes ownership of raw_ptr
```

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

This example demonstrates how `.reset()` releases ownership of the managed object, deletes it, and sets the pointer to null:

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

can you also add the output block wrapped in the shell block

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 managed object is deleted and the pointer becomes null.
- The check `if (!ptr)` confirms the pointer was successfully reset.

## Codebyte Example

Run the following example to understand how the `.reset()` method works:

```codebyte/cpp
#include <iostream>
#include <memory>
#include <string>

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<TV> 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;
}
```