AUTOSAR C++14 Rule A18-5-10
Placement new shall be used only with properly aligned pointers to sufficient storage capacity
Since R2020b
Description
Rule Definition
Placement new shall be used only with properly aligned pointers to sufficient storage capacity.
Rationale
The new
operator allocates the required amount of memory for storing
an object on the heap and constructs a new object in the allocated memory in a single
operation. If you want to separate the allocation and the construction and place an object
in preallocated memory on either the stack or the heap, you use placement
new
. Placement new
has advantages over
new
in certain situations, for example, when you need to place the
object at a known memory location.
The new
operator automatically allocates the correct amount of
aligned memory that the object requires. But when using placement new
,
you must manually make sure that the pointer you pass has sufficient allocated storage
capacity and is properly aligned. Violating these constraints results in the construction of
an object at a misaligned location or memory initialization outside of allocated bounds,
which might lead to unexpected or implementation-dependent behavior.
Polyspace Implementation
Suppose that a pointer ptr
is preallocated m
bytes
of memory on the stack and has alignment n
. For instance, if
ptr
is an array:
uint8_t ptr[5];
sizeof(uint8_t) * 5
and the alignment is
alignof(uint8_t)
. If you allocate more than m
bytes
to this pointer in a placement new
expression or if the alignment
required for the allocation is greater than n
, the checker raises a
violation. When determining the pointer alignment, the checker takes into account explicit
alignments such as with std::align
.The checker does not consider pointers that are preallocated memory on the heap since the available storage depends on the memory availability, which is known only at run time.
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, Automated |
Version History
Introduced in R2020b