AUTOSAR C++14 Rule A18-1-4
A pointer pointing to an element of an array of objects shall not be passed to a smart pointer of single object type
Since R2022a
Description
Rule Definition
A pointer pointing to an element of an array of objects shall not be passed to a smart pointer of single object type.
Rationale
You must deallocate pointers to array elements by using delete[]
instead of delete.
A pointer to an array element being passed to a smart pointer of single object type results in undefined behavior. Consider this code:
typedef A cArr[10];
std::unique_ptr<A> smartPtr1{new cArr}; //Noncompliant cArr and its elements require delete[]. However,
smartPtr1 attempts to deallocate by using delete,
resulting in undefined behavior.
Consider the following alternatives:
Avoid using smart pointers to a pointer to an item in an array of objects. Instead use:
std::arraystd::vectorstd::shared_ptr<std::vector<T>>
std::unique_ptr<T[]>and the corresponding overloads forstd::make_unique.As of C++17, you can use
std::shared_ptr<T[]>. The corresponding overloads forstd::make_sharedare not introduced until C++20.
Creating a custom deleter capable of handling an array of objects for the smart pointer
of a single object type is considered noncompliant with this rule. This alternative can be
error-prone, might no longer be supported in C++17, and is superseded by alternatives such
as std::unique_ptr<T[]>.
Polyspace Implementation
Polyspace® raises this defect when you pass a pointer pointing to an element in an array of objects to a smart pointer of a single object. Polyspace also raises this defect if you pass a C-style array to a smart pointer of a single object.
Polyspace raises this defect when these conditions are met:
You create a smart pointer by using
std::unique_ptr<T>orstd::shared_ptr<T>.You create an array of objects by using a C-style array or you create an array by using
std::make_uniqueorstd::make_shared.You use a function member of the smart pointer such as
release()orget()to obtain the pointer to pass to the smart pointer.
You can pass the pointer to a smart pointer in several ways, including using a copy
constructor, move constructor, or the reset() member function of the
smart pointer.
When using a copy or move constructor, Polyspace flags the checker on the declared object name. In the case of a
reset() member function, Polyspace flags the checker on the reset() member function.
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 R2022a