Skip to content

Commit 6d21a04

Browse files
committed
Add note on array usage.
Closes #15
1 parent 0c78392 commit 6d21a04

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

04-Considering_Safety.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,23 @@ delete myobj;
5656
5757
5858
// Good Idea
59-
std::shared_ptr<MyClass> myobj = make_shared<MyClass>();
59+
auto myobj = std::make_unique<MyClass>(); // C++14
60+
auto myobj = std::unique_ptr<MyClass>(new MyClass()); // C++11
61+
62+
// or for reference counted objects
63+
64+
auto myobj = std::make_shared<MyClass>();
65+
6066
// ...
6167
// myobj is automatically freed for you whenever it is no longer used.
6268
```
6369

70+
## Use `std::array` or `std::vector` Instead of C-style Arrays
71+
72+
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.
73+
74+
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.
75+
6476
## Use Exceptions
6577

6678
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.

0 commit comments

Comments
 (0)