0
0
CppHow-ToBeginner · 2 min read

C++ How to Convert Raw Pointer to Smart Pointer

To convert a raw pointer to a smart pointer in C++, use std::unique_ptr<T> ptr(rawPtr); for unique ownership or std::shared_ptr<T> ptr(rawPtr); for shared ownership, where rawPtr is your raw pointer.
📋

Examples

Inputint* rawPtr = new int(10);
Outputstd::unique_ptr<int> ptr(rawPtr); // ptr now owns the int
InputMyClass* rawPtr = new MyClass();
Outputstd::shared_ptr<MyClass> ptr(rawPtr); // shared ownership of MyClass
Inputint* rawPtr = nullptr;
Outputstd::unique_ptr<int> ptr(rawPtr); // ptr is empty smart pointer
🧠

How to Think About It

When you have a raw pointer from new, you can transfer its ownership to a smart pointer to manage memory automatically. Decide if you want unique ownership with std::unique_ptr or shared ownership with std::shared_ptr, then initialize the smart pointer directly with the raw pointer.
📐

Algorithm

1
Get the raw pointer from new or other source.
2
Choose the smart pointer type: unique_ptr for sole ownership, shared_ptr for shared ownership.
3
Initialize the smart pointer by passing the raw pointer to its constructor.
4
Use the smart pointer to manage the object's lifetime automatically.
💻

Code

cpp
#include <iostream>
#include <memory>

int main() {
    int* rawPtr = new int(42);
    std::unique_ptr<int> smartPtr(rawPtr);
    std::cout << "Value: " << *smartPtr << std::endl;
    // No need to delete rawPtr manually
    return 0;
}
Output
Value: 42
🔍

Dry Run

Let's trace converting a raw pointer to std::unique_ptr.

1

Create raw pointer

rawPtr points to an int with value 42.

2

Initialize unique_ptr

smartPtr now owns the int pointed by rawPtr.

3

Access value

*smartPtr returns 42.

StepPointerValueOwnership
1rawPtr42raw pointer owns memory
2smartPtr42unique_ptr owns memory, rawPtr should not be used
3smartPtr42access value via smartPtr
💡

Why This Works

Step 1: Transfer ownership

Passing the raw pointer to the smart pointer constructor transfers ownership, so the smart pointer manages deletion.

Step 2: Automatic memory management

The smart pointer automatically deletes the object when it goes out of scope, preventing memory leaks.

Step 3: Avoid double deletion

After transferring ownership, do not delete the raw pointer manually to avoid double deletion errors.

🔄

Alternative Approaches

Use std::make_unique (preferred for new objects)
cpp
#include <memory>
int main() {
    auto ptr = std::make_unique<int>(42);
    return 0;
}
This creates a unique_ptr directly without raw pointers, safer and clearer.
Use std::shared_ptr with std::make_shared
cpp
#include <memory>
int main() {
    auto ptr = std::make_shared<int>(42);
    return 0;
}
Preferred way to create shared_ptr without raw pointers, more efficient and safer.

Complexity: O(1) time, O(1) space

Time Complexity

Creating a smart pointer from a raw pointer is a constant time operation with no loops.

Space Complexity

Smart pointers add a small fixed overhead for control blocks (shared_ptr) or just store the pointer (unique_ptr).

Which Approach is Fastest?

Using std::make_unique or std::make_shared is generally faster and safer than converting raw pointers because they avoid separate allocations.

ApproachTimeSpaceBest For
Convert raw pointer to unique_ptrO(1)O(1)When you already have a raw pointer and want unique ownership
Convert raw pointer to shared_ptrO(1)O(1)When you already have a raw pointer and want shared ownership
Use std::make_uniqueO(1)O(1)Creating new objects with unique ownership, safer
Use std::make_sharedO(1)O(1)Creating new objects with shared ownership, more efficient
💡
Always prefer std::make_unique or std::make_shared to create smart pointers instead of converting raw pointers.
⚠️
Manually deleting a raw pointer after transferring it to a smart pointer causes double deletion and crashes.