Synopsis: | Use an array class instead of built-in arrays |
Language: | C++ |
Severity Level: | 9 |
Category: | Parts of C++ to Avoid |
Description: |
Especially the std::vector class template is usually a better alternative than built-in arrays for several reasons:
The most compelling reason to use a vector is that it frees you from explicit memory management, and it does not leak memory. A vector keeps track of the memory it uses to store its elements. When a vector needs more memory for elements, it allocates more; when a vector goes out of scope, it frees that memory. Therefore, the user needs not be concerned with the allocation and deallocation of memory for vector elements. An exception to this rule are built-in arrays that are used in a local scope. |