Mentions légales du service

Skip to content
Snippets Groups Projects

Add a paragraph about smart pointers efficiency, citing the reference given in MR29.

1 file
+ 14
0
Compare changes
  • Side-by-side
  • Inline
This simplifies the reading, especially if templates are also involved...
This simplifies the reading, especially if templates are also involved...
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
 
## The cost of using smart pointers
 
 
This [article](https://www.modernescpp.com/index.php/memory-and-performance-overhead-of-smart-pointer) provides an analysis of the cost involved both in performance and memory when using a smart pointer. To put in the nutshell, its conclusions are:
 
 
- `std::unique_ptr` bears no overhead in memory (except in a very edge case you can safely ignore for now) and little overhead in performance, at least for the latter when compiler optimizations are enabled (we will talk about them in a later [notebook](../6-InRealEnvironment/3-Compilers.ipynb)).
 
- `std::shared_ptr` incurs a memory overhead (to keep the reference count) and a performance overhead as well (which is partially mitigated if the memory was allocated through `std::make_shared`). The performance overhead is especially important when no optimizations are involved.
 
 
This highlights once more what we said earlier:
 
 
- Use smart pointers: RAII is simply too precious to manage properly your ressources!
 
- Use `std::unique_ptr` wherever you can, and use `std::shared_ptr` when you really need several instances of the same ressource.
 
%% Cell type:markdown id: tags:
Loading