description | title | ms.date | f1_keywords | helpviewer_keywords | |||
---|---|---|---|---|---|---|---|
Learn more about: Warning C6398: The address-of a field cannot be null in well-defined code |
Warning C6398 |
02/05/2024 |
|
|
The address-of a field cannot be
null
in well-defined code
The address-of operator returns the address of its operand. This value should never be compared to nullptr
:
- The address-of a field can only be
nullptr
if the base pointer wasnullptr
and the field is at the zero offset (&p->field == nullptr
impliesp == nullptr
). In this case, the expression should be simplified. - In any other cases, the value of the unary
&
operator can't benullptr
unless there's undefined behavior in the code (&v == nullptr
always evaluates to false).
struct A { int* x; };
bool hasNullField(A *a)
{
return &a->x == nullptr; // C6398 reported here.
}
To fix this issue, double check if the use of unary &
was intentional:
struct A { int* x; };
bool hasNullField(A *a)
{
return a->x == nullptr; // no C6398 reported here.
}