Mixing Smart and Raw Pointers

Is it okay to mix smart pointers and raw pointers in the same program?

Yes, it is common and often necessary to mix smart pointers and raw pointers in the same program. The key is to establish clear conventions around ownership and responsibility.

When a function uses a raw pointer, it is typically signaling that it wants access to the resource, but is not taking ownership of it. For example:

#include <memory>
#include <iostream>

void PrintName(std::string* Name) {
  std::cout << *Name << '\n';
}

int main() {
  auto Name{std::make_unique<std::string>(
    "Gandalf")};
  PrintName(Name.get());
}
Gandalf

Here, main is maintaining ownership of the resource, and PrintName is simply accessing it.

Conversely, when ownership needs to be transferred, smart pointers are used:

#include <memory>
#include <utility>
#include <iostream>

void StoreNameElsewhere(std::unique_ptr<
  std::string> Name) {
  // Store name in file, database etc
  std::cout << "Storing " << *Name << "\n";
}

int main() {
  auto Name{std::make_unique<std::string>(
    "Gandalf")};
  StoreNameElsewhere(std::move(Name));
}
Storing Gandalf

In summary:

  • Use smart pointers for ownership
  • Use raw pointers for non-owning access
  • Be clear about whether a function is taking ownership or not
  • Don't delete resources you don't own

Smart Pointers and std::unique_ptr

An introduction to memory ownership using smart pointers and std::unique_ptr in C++

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Should I Pass Smart Pointers by Reference?
Should I pass smart pointers by value or reference?
Dynamically Allocating Arrays with Smart Pointers
How do I dynamically allocate an array with smart pointers?
Using Smart Pointers with Custom Deleters
Can I use smart pointers with my own custom deleters?
std::make_unique vs new Keyword
What are the advantages of using std::make_unique over the new keyword?
Using std::move with std::unique_ptr
What does std::move do when used with std::unique_ptr?
Using std::unique_ptr as a Class Member
How do I use std::unique_ptr as a member of a class?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant