Memory leak? IMO, a vector in combination with iterators does not protect you from errors, such as going beyond or using an invalid iterator in general (unless you have VC ++ with iterator debugging); rather, it is convenient because it implements a dynamically changing array for you and takes care of memory management (NB! helps to make your code more safe for exceptions).
void foo(const char* zzz) { int* arr = new int[size]; std::string s = zzz;
The above may occur if an exception occurs (for example, when creating a string). Not with a vector.
The vector also simplifies the explanation of the code because of its semantics of meaning.
int* arr = new int[size]; int* second_ref = arr;
But, perhaps, the vector does not automatically satisfy 100% of cases of using dynamic arrays. (For example, there is also boost::shared_array and to-be std::unique_ptr<T[]> )
Unclebens
source share