AutosarC++18_10-A18.5.11¶
“operator new” and “operator delete” shall be defined together
Required inputs: IR
new/delete and
new[]/delete[]) should always be declared together in the same scope.
If only one is defined, the compiler silently falls back to a mismatched allocator/deallocator pair,
which is undefined behavior. Both operators should also have consistent visibility.
Bad code (operator new without matching operator delete):
class MyClass {
public:
void* operator new(size_t size); // ERROR: no matching operator delete;
// ::operator delete will be used instead
};
Good code (both declared in same scope):
class MyClass {
public:
void* operator new(size_t size);
void operator delete(void* ptr); // OK: paired in the same scope
};
Bad code (mixed visibility in same class):
class MyClass {
public:
void* operator new(size_t size); // public
private:
void operator delete(void* ptr); // ERROR: inconsistent visibility
};
Good code (consistent visibility):
class MyClass {
public:
void* operator new(size_t size);
void operator delete(void* ptr); // OK: same scope, same visibility
};
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
array_delete_missing |
Scope contains operator new[] but not operator delete[] |
None |
False |
array_mixed_visibility |
Scope contains both new[] and delete[] operators but the visibility differs |
None |
False |
array_new_missing |
Scope contains operator delete[] but not operator new[] |
None |
False |
delete_missing |
Scope contains operator new but not operator delete |
None |
False |
mixed_visibility |
Scope contains both new and delete operators but visibility differs |
None |
False |
new_missing |
Scope contains operator delete but not operator new |
None |
False |
Options
This rule shares the following common options: exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
This rule has no individual options.