Lưu ý
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử đăng nhập hoặc thay đổi thư mục.
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử thay đổi thư mục.
The address-of a field cannot be
nullin well-defined code
Remarks
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
nullptrif the base pointer wasnullptrand the field is at the zero offset (&p->field == nullptrimpliesp == nullptr). In this case, the expression should be simplified. - In any other cases, the value of the unary
&operator can't benullptrunless there's undefined behavior in the code (&v == nullptralways evaluates to false).
Example
The following example generates C6398:
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.
}