Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Providing Custom Deleters For Smart Pointers | Advanced topics
course content

Conteúdo do Curso

C++ Smart Pointers

Providing Custom Deleters For Smart PointersProviding Custom Deleters For Smart Pointers

We already know that smart pointers can automatically call the destructors of the dynamic objects when the objects are no longer needed. But what if we wanted to call a different function to destroy a dynamic object and properly clean the allocated memory? This is where custom deleters come into play.

What are custom deleters?

A Custom deleter is a function or a function object that is associated with a smart pointer during its creation. It defines how the pointed-to object should be destroyed when the smart pointer goes out of scope. Custom deleters enable more granular control over the resource cleanup process. This behavior allows developers to efficiently deallocate memory for a wide array of resources, including file handles, connections, and custom data structures.

Practical use cases

Let's consider a practical example to realize the importance of custom deleters. Suppose you are working on a project that involves handling files with smart pointers. You want to ensure that when a smart pointer goes out of scope, it also closes the associated file. This will ensure two things: the dynamic memory is deallocated AND the file is properly closed at the same time. Here’s how we can do both with a custom deleter:

cpp

main.cpp

While creating the unique pointer, we pass the customFileDeleter function, our custom deleter, as a template parameter. The decltype keyword is just used to extract the type of the function at compile time. The customFileDeleter function allows us to combine memory deallocation and file closure inside a single function.

Shared pointers and custom deleters

To associate a custom deleter with a shared pointer, we have to pass the deleter to the constructor of the shared pointer. Let’s look at a simple example:

cpp

main.cpp

In the above example, we are creating a shared pointer to an integer and passing the custom deleter function as an argument to the constructor.

When should we use a custom deleter for a shared pointer?

Selecione a resposta correta

Tudo estava claro?

Seção 6. Capítulo 1
course content

Conteúdo do Curso

C++ Smart Pointers

Providing Custom Deleters For Smart PointersProviding Custom Deleters For Smart Pointers

We already know that smart pointers can automatically call the destructors of the dynamic objects when the objects are no longer needed. But what if we wanted to call a different function to destroy a dynamic object and properly clean the allocated memory? This is where custom deleters come into play.

What are custom deleters?

A Custom deleter is a function or a function object that is associated with a smart pointer during its creation. It defines how the pointed-to object should be destroyed when the smart pointer goes out of scope. Custom deleters enable more granular control over the resource cleanup process. This behavior allows developers to efficiently deallocate memory for a wide array of resources, including file handles, connections, and custom data structures.

Practical use cases

Let's consider a practical example to realize the importance of custom deleters. Suppose you are working on a project that involves handling files with smart pointers. You want to ensure that when a smart pointer goes out of scope, it also closes the associated file. This will ensure two things: the dynamic memory is deallocated AND the file is properly closed at the same time. Here’s how we can do both with a custom deleter:

cpp

main.cpp

While creating the unique pointer, we pass the customFileDeleter function, our custom deleter, as a template parameter. The decltype keyword is just used to extract the type of the function at compile time. The customFileDeleter function allows us to combine memory deallocation and file closure inside a single function.

Shared pointers and custom deleters

To associate a custom deleter with a shared pointer, we have to pass the deleter to the constructor of the shared pointer. Let’s look at a simple example:

cpp

main.cpp

In the above example, we are creating a shared pointer to an integer and passing the custom deleter function as an argument to the constructor.

When should we use a custom deleter for a shared pointer?

Selecione a resposta correta

Tudo estava claro?

Seção 6. Capítulo 1
some-alt