TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C++ Coding Standard
Search

Rule:  PCA#009Checked automatically with code checker

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:

  • They allocate memory from the free space when increasing in size.
  • They are NOT a pointer in disguise.
  • They can increase/decrease in size run-time.
  • They can do range checking using at().
  • A vector knows its size, so you don't have to count elements.

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.