AUTOSAR C++14 Rule A18-5-3
The form of delete operator shall match the form of new operator used to allocate the memory
Description
Rule Definition
The form of delete operator shall match the form of new operator used to allocate the memory..
Rationale
The
delete
operator releases a block of memory allocated on the heap. If you try to access a location on the heap that you did not allocate previously, a segmentation fault can occur.If you use the single-object notation for
delete
on a pointer that is previously allocated with the array notation fornew
, the behavior is undefined.
The issue can also highlight other coding errors. For instance, you perhaps wanted to
use the delete
operator or a previous new
operator on
a different pointer.
Polyspace Implementation
The checker flags a defect when:
You release a block of memory with the
delete
operator but the memory was previously not allocated with thenew
operator.You release a block of memory with the
delete
operator using the single-object notation but the memory was previously allocated as an array with thenew
operator.
This defect applies only to C++ source files.
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 R2019a