Rule Definition
'std::vector' automatically manages memory, including calling 'delete[]' when required, even when exceptions are thrown. Moreover, 'std::vector' provides more functionalities, including the ability to resize itself dynamically.
For dynamically allocated arrays of char type, consider using 'std::string' instead of 'std::vector', since this type contains specific functions for string manipulations.
For more information, refer to Effective STL Item 13 from Scott Meyers.
Remediation
You can use either 'std::vector', if the size of collection is not known at compile time, or 'std::array' if it is a compile-time constant (for instance, storing coordinates in a three dimensional world).
Violation Code Sample
void f()
{
MyClass *myCol = new MyClass[100] ;
delete [] myCol ;
char *myText = new char[10] ;
strcpy (myText, "Hello world!");
delete[] myText;
}
Fixed Code Sample
void f()
{
std::vector<MyClass> myCol(100) ;
std::string myText = "Hello world !";
}
Related Technologies
C++
Technical Criterion
Complexity - Dynamic Instantiation
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.