Solved Violation of MISRA08_18-4-1
-
Hi,
In the header file I have declared a address variable as
Animal *m_lion;
and in implementation file I have written this
m_lion = new Lion;
where Animal is the base class with some virtual functions and Lion is its derived class.
I am deallocating the memory in the destructor of the class in which i have declared and allocated the memory for the address variableIf I run the static code analyser with MISRA2008,then I am getting error as Violation of MISRA08_18-4-1
Looking forward for help,
with regards -
@Phadnis said in Violation of MISRA08_18-4-1:
Violation of MISRA08_18-4-1
Is there more than that? Some descriptive text?
As far as I know MISRA disallows any kind of dynamic memory allocation (malloc/new). -
@Phadnis said in Violation of MISRA08_18-4-1:
MISRA08_18-4-1
As far as I know MISRA disallows any kind of dynamic memory allocation (malloc/new).
<This Link> verifies your suspicion.
-
Rationale
The use of dynamic memory can lead to out-of-storage run-time failures, which are undesirable. The built-in new and delete operators, other than the placement versions, use dynamic heap memory. The functions calloc, malloc, realloc and free also use dynamic heap memory. There is a range of unspecified, undefined and implementation-defined behaviour associated with dynamic memory allocation, as well as a number of other potential pitfalls. Dynamic heap memory allocation may lead to memory leaks, data inconsistency, memory exhaustion, non-deterministic behaviour, etc.
Note that some implementations may use dynamic heap memory allocation to implement other functions (for example, functions in the library cstring). If this is the case, then these functions shall also be avoided.
Example
void f1 ( )
{
int32_t * i = new int32_t; // Non-compliant
delete i;
}- Below is the code
User::User()
{
m_lion = new Lion;
}User::~User()
{
delete m_lion;
} -
@Phadnis said in Violation of MISRA08_18-4-1:
Rationale
If you have drank the cool-aid and don't intend to allow free-store allocation of objects then you cannot use Qt. It's really that simple. No mention of whether the end app is for an embedded platform, but I'd suspect it's more or a training or academic exercise for you. You are buying into a MISRA religious dogma without having a greater understanding of the context in which the rules are important. They don't apply across the board, but only for specialized cases.
-
@Kent-Dorfman Thanks I agree.