AutosarC++17_10-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
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
Memory allocated for array type needs to be deallocated using delete[] syntax. Shared pointers do not have such information, and it is not possible to pass a custom array deleter to std::make_shared function.
An std::array or std::vector can be used instead of the raw array type.
Note that it is allowed to use the std::unique_ptr with T[] template argument.
Example
// $Id: A18-1-4.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <array>
#include <memory>
#include <vector>
class A {};
void F1() noexcept(false) {
A cArray[10]; // Compliant
std::shared_ptr<A> ptr1(
cArray); // Non-compliant - ptr1 will not delete all of its elements
std::shared_ptr<A> ptr2(
new A[10]); // Non-compliant - ptr2 will not delete all of its elements
// std::shared_ptr<A[]> ptr3(new A[10]); // Non-compliant - compilation
// error
std::shared_ptr<std::array<A, 10>> ptr4 =
std::make_shared<std::array<A, 10>>(); // Compliant
std::shared_ptr<std::vector<A>> ptr5 =
std::make_shared<std::vector<A>>(10, A()); // Compliant
}
void F2() noexcept(false) {
// std::unique_ptr<A> ptr1 = std::make_unique<A>(10); // Non-compliant - no
// such constructor in class A
std::unique_ptr<A> ptr2{
new A[10]}; // Non-compliant - ptr2 will not delete all of its elements
std::unique_ptr<A[]> ptr3 =
std::make_unique<A[]>(10); // Compliant - std::unique_ptr provides a
// specialization for T[] types
}
See Also
- HIC++ v4.0 [8]: 17.3.4 Do not create smart pointers of array type.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
type_used |
{} shall not refer to an array type. |
None |
False |
Options
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
This rule has no individual options.