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

std::unique_ptr and std::shared_ptr are designed for single objects, not arrays. Passing array pointers to single-object smart pointers leads to undefined behavior: the destructor calls delete instead of delete[], causing memory leaks and heap corruption. Use std::unique_ptr with array specialization or std::vector instead.
Bad code (single-object smart pointer with array):
int arr[10];
std::unique_ptr<int> ptr(arr);     // ERROR: will call delete instead of delete[]
// Undefined behavior at destruction: heap corruption likely
Good code (array specialization):
std::unique_ptr<int[]> ptr(new int[10]);  // OK: array specialization calls delete[]
// Correct cleanup guaranteed
Good code (using std::vector):
std::vector<int> vec(10);  // OK: vector manages lifetime automatically
// Clean, exception-safe

Possible Messages

Key

Text

Severity

Disabled

type_used

{} shall not refer to an array type.

None

False

Options