AutosarC++19_03-A18.5.11

“operator new” and “operator delete” shall be defined together

Required inputs: IR

Allocation and deallocation operators (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