|
|
<ol><li><strong>Bare pointers:</strong> Avoid bare pointers (*p) at all cost for memory management with new and delete. Prefer objects, references to objects, smart pointers: unique_ptr, shared_ptr , and the standard template library (STL) containers (<em>e.g.</em> std::vector , std::map , std::queue ) when corresponding. Also, use smart pointers for polymorphism and/or to allow nullptr as a valid state (<em>e.g.</em> waiting for message). Exception: in interactions or wrapper to C libraries functions (<em>e.g.</em> compression).<ul><li><code>Don't</code> <ul><li><pre style="border: 0;"><code> double *variable = new double [Nx]; </code></pre> </li></ul></li><li><code>Do</code> <ul><li><pre style="border: 0;"><code> std::vector < double > variable( Nx ); </code></pre></li></ul></li></ul></li>
|
|
|
1. <strong>Avoid ```new/delete``` bare pointers</strong>: use objects, references to objects, smart pointers: ```unique_ptr, shared_ptr,``` and the standard template library (STL) containers (<em>e.g.</em> ```std::vector, std::map, std::queue``` ) when corresponding for automatic memory management. In addition, use smart pointers for polymorphism and/or to allow ```nullptr``` as a valid state (<em>e.g.</em> waiting for message). Only use bare pointers as "reference" pointers, not to manage memory manually.
|
|
|
|
|
|
<li><strong>Bare Pointers in STL containers:</strong> Avoid storing allocated (with new) bare pointers in STL containers, they must be deallocated manually if so (with delete). The STL is already taking care of the object memory. Bare pointers used as references (not managing memory) are acceptable.
|
|
|
<ul><li><code>Don't</code> <ul><li><pre style="border: 0;"><code>
|
|
|
// shouldn't be used in the first place
|
|
|
double * foo = new double [100];
|
|
|
// foo needs delete, vector won't deallocate content
|
|
|
std::vector < double* > variable { foo };
|
|
|
</code></pre> </li></ul></li></ul></li>
|
|
|
* _Don't_
|
|
|
```cpp
|
|
|
double *variable = new double [Nx];
|
|
|
```
|
|
|
* _Do_
|
|
|
```cpp
|
|
|
std::vector<double> variable(Nx);
|
|
|
|
|
|
<li><strong>std::move:</strong> C++11 contains overloaded functions that automatically identify an object's move constructor (&&) (<em>e.g.</em> STL containers, std::string). Only use std::move explicitly when it is not clear if the copy (&) or move constructor (&&) will be invoked (<em>e.g.</em> large custom objects moved to a container, in that case prefer emplace_back). </li>
|
|
|
```
|
|
|
|
|
|
<li><strong>Use RAII:</strong> resource allocation is initialization. Constructors must allocate, while destructors will take care of all object deallocations and system resources (<em>e.g.</em> file, shared memory). Fine tuning memory should be used only if it causes a big impact (<em>e.g.</em> large data). STL containers and smart pointers are RAII complaint. If a class only contains RAII members destructors should be empty.</li></ol> |
|
|
\ No newline at end of file |
|
|
2. <strong>```std::move``` and C++11</strong>: C++11 adds overloaded standard functions and constructors that automatically identify an object's move constructor (```&&```) (_e.g._ STL containers element insertion, deletion). Only use std::move explicitly when it is not clear if the copy (```&```) or move (```&&```) constructor will be called (_e.g._ large user-defined objects moved to a container, in that case prefer emplace_back).
|
|
|
|
|
|
3. <strong>Use RAII</strong>: resource allocation is initialization. Constructors must allocate, while destructors will take care of all object deallocations and system resources (_e.g._ file, shared memory). Fine tuning memory should be used only if it causes a big impact. STL containers and smart pointers are RAII complaint. If a class only contains RAII members destructors should be empty or declared with the keyword ```default```. |
|
|
\ No newline at end of file |