Operator new not overloaded for possibly over-aligned class
Allocated storage might be smaller than object alignment requirement
Description
This defect occurs when you do not adequately overload operator
new/new[]
and you use this operator to create an object with an alignment
requirement specified with alignas
. The checker raises a defect for these
versions of throwing and non-throwing operator new/new[]
.
void* operator new(std::size_t size)
void* operator new(std::size_t size, const std::nothrow_t&)
void* operator new[](std::size_t size)
void* operator new[](std::size_t size, const std::nothrow_t&)
The use of alignas
indicates that you do not expect the
default operator new/new[]
to satisfy the alignment requirement or the
object, and that the object is possibly over aligned. A type is over aligned if you use
alignas
to make the alignment requirement of the type larger than
std::max_align_t
. For instance, foo
is over aligned in
this code snippet because its alignment requirement is 32 bytes, but
std::max_align_t
has an alignment of 16 bytes in most implementations.
struct alignas(32) foo { char elems[32]; }
Operator new not overloaded for possibly overaligned
class raises no defect if you do not overload the operator
new/new[]
and you use version C++17 or later of the Standard. The default
operator new/new[]
in C++17 or later supports over alignment by passing the
alignment requirement as an argument of type std::align_val_t
, for instance
void* operator new(std::size_t size, std::align_val_t alignment)
.
Risk
The default operator new/new[]
allocates storage with the alignment
requirement of std::align_val_t
at most. If you do not overload the
operator when you create an object with over aligned type, the resulting object may be
misaligned. Accessing this object might cause illegal access errors or abnormal program
terminations.
Fix
If you use version C++14 or earlier of the Standard, pass the alignment requirement of
over aligned types to the operator new/new[]
by overloading the
operator.
Examples
Result Information
Group: Object Oriented |
Language: C++ |
Default: On for handwritten code, off for generated code |
Command-Line Syntax:
MISSING_OVERLOAD_NEW_FOR_ALIGNED_OBJ |
Impact: Medium |
Version History
Introduced in R2019b
See Also
Find defects
(-checkers)
| Missing overload of allocation or deallocation function
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)