CPU-malloc-class (C++ only)
In this section:
Synopsis
An allocation of a class instance with malloc() does not call a constructor.
Enabled by default
Yes
Severity/Certainty
Low/High

Full description
When allocating memory for a class instance with malloc(), no class constructor is called. Using malloc() creates an uninitialized object. To initialize the object at allocation, use the new operator
Coding standards
This check does not correspond to any coding standard rules.
Code examples
The following code example fails the check and will give a warning:
#include <stdlib.h>
class Foo {
public:
void setA(int val){
a=val;
}
private:
int a;
};
void main(){
Foo *fooArray;
//malloc of class Foo
fooArray = static_cast<Foo*>(malloc(5 * sizeof(Foo)));
fooArray->setA(4);
}
The following code example passes the check and will not give a warning about this issue:
#include <stdlib.h>
void main(){
int *fooArray;
fooArray = static_cast<int*>(malloc(5 * sizeof(int)));
*fooArray = 4;
}