Main Content

AUTOSAR C++14 Rule A4-10-1

Only nullptr literal shall be used as the null-pointer-constraint

Since R2020a

Description

Rule Definition

Only nullptr literal shall be used as the null-pointer-constraint.

Rationale

nullptr was introduced in C++11 to support the concept of a pointer that does not point to a valid object. Before C++11, the macro NULL and the constant 0 were the only ways to define the null pointer constant. Using nullptr to indicate null-pointers has several advantages over using NULL or 0. For instance:

  • nullptr can be used with any type of null-pointer without requiring an implicit cast.

  • nullptr literals allow parameter forwarding by using a template function.

NULL is a macro that expands to an integer 0 which is cast into void* type. Using NULL or 0 to indicate null-pointers is contrary to developer expectation. If code expecting nullptr encounters NULL or 0 instead, it might lead to confusion or unexpected behavior.

Polyspace Implementation

Polyspace® flags the use of NULL or 0 instead of nullptr to indicate a null-pointer. This rule does not check for conversion between NULL and 0. See AUTOSAR C++14 Rule M4-10-1.

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

expand all

#include <cstdint>
#include <cstddef>


void foo(int*);
void foo2(int*);

void bar() {
    foo(NULL);    //Noncompliant
    foo2(0);      //Noncompliant
    foo(nullptr); //Compliant
}

In this example, the rule is violated when the macro NULL or the constant 0 is used as a null-pointer instead of nullptr.

Check Information

Group: Standard conversions
Category: Required, Automated

Version History

Introduced in R2020a