MISRA C++:2023 Rule 21.6.4
If a project defines either a sized or unsized version of a global operator
delete
, then both shall be defined
Since R2024b
Description
Rule Definition
If a project defines either a sized or unsized version of a global
operator delete
, then both shall be defined.
Rationale
The C++14 Standard defines a sized version of operator delete
. For
instance, for an unsized operator delete
with this
signature:
void operator delete (void* ptr);
void operator delete (void* ptr, std::size_t size);
operator delete
.The Standard states that if both versions of operator delete
exist,
the sized version must be called because it provides a more efficient way to deallocate
memory. However, in some cases, for instance to delete incomplete types, the unsized version
is used.
If you overload the unsized version of operator delete
, you must also
overload the sized version. You typically overload operator delete
to
perform some bookkeeping in addition to deallocating memory on the free store. If you
overload the unsized version but not the sized one or the other way around, any bookkeeping
you perform in one version will be omitted from the other version. This omission can lead to
unexpected results.
Polyspace Implementation
The checker flags situations where an unsized version of operator
delete
exists but the corresponding sized version is not defined, or vice
versa.
The checker is enabled only if you specify a C++ version of C++14 or later. See
C++ standard version (-cpp-version)
.
Troubleshooting
If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Language support library |
Category: Required |
Version History
Introduced in R2024b