Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Introduction to Unique Pointers | Unique Pointers
C++ Smart Pointers

bookIntroduction to Unique Pointers

To create a std::unique_ptr you can use the std::make_unique. It is a function that provides a safe and efficient way to create unique pointers.

It's a great alternative to using the new/delete and new[]/delete[] operators, which can make code more susceptible to memory-related issues.

make_unique.h

make_unique.h

copy
12
// Using `std::make_unique` to create a unique pointer to a vector of integers std::unique_ptr<std::vector<int>> p_unique_vector = std::make_unique<std::vector<int>>();

The std::make_unqiue allocates the dynamic resource and returns a unique pointer that owns it. When the unique pointer goes out of scope, the resource is automatically deallocated.

new_unique_ptr.h

new_unique_ptr.h

copy
12
// Insecure way to create a unique pointer for a vector of integers. std::unique_ptr<std::vector<int>> p_unique_vector(new std::vector<int>());

The p_unique_vector vector in the above code will still be automatically destroyed when the unique pointer goes out of scope. However, for maximum exception safety and better code practices, always prefer using std::make_unique.

question mark

What is the main advantage of using std::make_unique over new when creating a std::unique_ptr?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 1

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 5.56

bookIntroduction to Unique Pointers

Scorri per mostrare il menu

To create a std::unique_ptr you can use the std::make_unique. It is a function that provides a safe and efficient way to create unique pointers.

It's a great alternative to using the new/delete and new[]/delete[] operators, which can make code more susceptible to memory-related issues.

make_unique.h

make_unique.h

copy
12
// Using `std::make_unique` to create a unique pointer to a vector of integers std::unique_ptr<std::vector<int>> p_unique_vector = std::make_unique<std::vector<int>>();

The std::make_unqiue allocates the dynamic resource and returns a unique pointer that owns it. When the unique pointer goes out of scope, the resource is automatically deallocated.

new_unique_ptr.h

new_unique_ptr.h

copy
12
// Insecure way to create a unique pointer for a vector of integers. std::unique_ptr<std::vector<int>> p_unique_vector(new std::vector<int>());

The p_unique_vector vector in the above code will still be automatically destroyed when the unique pointer goes out of scope. However, for maximum exception safety and better code practices, always prefer using std::make_unique.

question mark

What is the main advantage of using std::make_unique over new when creating a std::unique_ptr?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 1
some-alt