Add note on array usage.

Closes #15
This commit is contained in:
Jason Turner
2015-06-08 10:23:12 -06:00
parent 0c783920ab
commit 6d21a04e79

View File

@@ -56,11 +56,23 @@ delete myobj;
// Good Idea // Good Idea
std::shared_ptr<MyClass> myobj = make_shared<MyClass>(); auto myobj = std::make_unique<MyClass>(); // C++14
auto myobj = std::unique_ptr<MyClass>(new MyClass()); // C++11
// or for reference counted objects
auto myobj = std::make_shared<MyClass>();
// ... // ...
// myobj is automatically freed for you whenever it is no longer used. // myobj is automatically freed for you whenever it is no longer used.
``` ```
## Use `std::array` or `std::vector` Instead of C-style Arrays
Both of these guarantee contiguous memory layout of objects and can (and should) completely replace your usage of C-style arrays for many of the reasons listed for not using bare pointers.
Also, [avoid](http://stackoverflow.com/questions/3266443/can-you-use-a-shared-ptr-for-raii-of-c-style-arrays) using `std::shared_ptr` to hold an array.
## Use Exceptions ## Use Exceptions
Exceptions cannot be ignored. Return values, such as using `boost::optional`, can be ignored and if not checked can cause crashes or memory errors. An exception, on the other hand, can be caught and handled. Potentially all the way up the highest level of the application with a log and automatic restart of the application. Exceptions cannot be ignored. Return values, such as using `boost::optional`, can be ignored and if not checked can cause crashes or memory errors. An exception, on the other hand, can be caught and handled. Potentially all the way up the highest level of the application with a log and automatic restart of the application.