5. ● First obvious thing...
– DO NOT MIX C and C++ code
● First obvious example - MySQL
● Garbage collection
– Managed C++
● http://managedcpp.sourceforge.net
● uses precise (depends on the type safety properties)
– BoehmGC
● http://www.hboehm.info/gc/
● uses conservative (slower)
– gcnew/gcdelete methods
6. Index out of bounds
#define N 10
T static_array[N];
int n = 20;
T* dynamic_array= new T[n];
std::vector<T> vector_array;
array[index] = x;
Safe C++ suggests:
1. overload the [] operator
2. use vectors
7. Index out of bounds
std::vector<T> vector_array;
Problem with vectors
- they are dynamically allocated
- they are allocating larger chunks during
initialization
- every time the limit of a vector is reached
a new one is created and old values are copied
8. Index out of bounds
* Do not use static or dynamically allocated arrays.
Use a template array or vector instead.
* Do not use new and delete operators with brackets,
leave it up to the template vector to allocate
multiple elements.
* Use scpp:vector instead of std::vector and
scpp::array consistently instead of a static array,
and switch the sanity checks on.
* For a multidimensional array, use scpp::matrix and
access elements through the () operator to provide
index-out-of-bounds checks.
10. Pointer arithmetic
vector<int> v;
for (int i=0; i<10; i++)
v.push_back(i);
int* ptr = &v[3];
same as
int& ptr = v[3];
vector<int>::const_iterator new1 = v.begin;
- this has the same issue as the previous
example
The same is true for almost all STL and STL like
containers.
hash_set & hash_map are also the same
11. Pointer arithmetic
* Do not hold pointers, references, or iterators to
the element of a container after you’ve modified the
container.
* Avoid pointer arithmetic(where possible). Use
template vector or array with index instead.
12. Uninitialized Variables
* Do not use built-in types such as int, unsigned,
double, bool, etc., for class data members.
- Instead, use Int, Unsigned, Double, Bool, etc.,
because you will not need to initialize them in
constructors.
* Use these new classes instead of built-in types
for passing parameters to functions, to get
additional type safety.
13. Uninitialized Variables
* auto_ptr / unique_ptr
- limited garbage collection by automatically
calling delete in the destructor
* also called smart pointers
* Scoped Pointers
- can't be copied
-
17. Dereferencing NULL pointers
* If you have a pointer that owns the object it
points to, use a smart pointer (a reference counting
pointer or scoped pointer).
* When you have a raw pointer T* pointing to an
object you do not own, use the template class Ptr<T>
instead.
* For a const pointer (i.e., const T*) use
Ptr<const T>.
18. Copy Constructors and
Assignment Operators
Each time you add a new data member in a class
do not forget to add corresponding code to the
copy-constructor and assignment operators!
If you DON'T... C++ creates “defaults” for you.
19. Copy Constructors and
Assignment Operators
* Rely on default versions created for you
automatically by a compiler.
* Prohibit copies of any kind by declaring the copy
constructor and assignment operator as private, and
do not provide an implementation.
* Write your own versions.
20. Copy Constructors and
Assignment Operators
* Whenever possible, avoid writing a copy-constructor
or assignment operator for your classes.
* If the default versions do not work for you,
consider prohibiting the copying of instances of your
class by declaring the copy-constructor and assignment
operator private.
21. Others
* DCL50-CPP. Do not define a C-style variadic
function
* EXP50-CPP. Do not depend on the order of evaluation
for side effects
* MEM51-CPP. Properly deallocate dynamically
allocated resources