Use STL containers instead of C arraysThis tip submitted by Sang-drax on 2004-12-29 00:00:00. It has been viewed 31654 times.
Rating of 6.0 with 174 votes
When using C++, you should always strive to use STL containers like std::vector and std::list instead of ordinary C arrays.
Consider this example:
int* array = new array[size]; doSomething(array); delete array;
All is fine, until doSomething() is changed to throw an exception. If doSomething throws, we will have a memory leak.
The correct way of doing this is:
If doSomething() throws an exception now, no memory will be leaked.
This is just one of the reasons to use STL containers instead of C arrays. STL containers are much less error prone, more robust and easier to use.
Help your fellow programmers! Add a tip!