最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Can & (address-of-operator) ever return nullptr? - Stack Overflow

programmeradmin0浏览0评论

Sounds like a trivial question judging from typical usage of & operator, but is there a guarantee from the standard that address-of-operator cannot return nullptr when trying to get an address?

Sounds like a trivial question judging from typical usage of & operator, but is there a guarantee from the standard that address-of-operator cannot return nullptr when trying to get an address?

Share Improve this question asked Nov 20, 2024 at 11:55 Alexey S. LarionovAlexey S. Larionov 7,9472 gold badges25 silver badges46 bronze badges 20
  • 9 It may happen if one has a silly overload of operator&, e.g. struct foo { foo* operator& () { return nullptr; } }; – edrezen Commented Nov 20, 2024 at 12:00
  • 3 If you had a null reference (which is undefined behaviour) then it could return null – Alan Birtles Commented Nov 20, 2024 at 12:00
  • 3 related to the first comment, consider that there is also std::addressof which one should use if you want to be sure to get an address rather than whatever a silly overload may return. Are you asking whether the address of an object can be nullptr or about &x specifically? – 463035818_is_not_an_ai Commented Nov 20, 2024 at 12:01
  • 2 Is this just curiosity, which is fine? Then please state so in the question. Otherwise please ask about the actual and underlying problem you have. – Some programmer dude Commented Nov 20, 2024 at 12:05
  • 5 If you consider low-level programming, you can run in a situation where you may need to deal with an object whose address coincides with the representation of the null pointer. For example, on certain CPU architectures, interrupt vectors are placed at address 0, which was also used to represent the null pointer. This means that a pointer may test equal to null, while not being semantically null. – ach Commented Nov 20, 2024 at 12:27
 |  Show 15 more comments

2 Answers 2

Reset to default 7

There is no guarantee that the operator& can't return nullptr, for instance by overloading this operator.

Here is a contrived example:

#include <iostream>

struct foo  { foo* operator& ()  { return nullptr; }  };

int main()
{
    foo f;
    
    if (&f == nullptr) {  std::cout << "nullptr detected\n";  }
    
    std::cout << "the true address is " << std::addressof(f) << "\n";
}

Demo

OP's question is specifically for &x (as OP mentioned in a comment).

Can & (address-of-operator) ever return nullptr?

No for built-in types and no for user defined types, if operator & is not overloaded. Otherwise, for user defined types, you can overload & operator and then the behavior will be based on your implementation of operator&, where you can return nullptr as well.

From C++17 (8.3.1) Unary operators [emphasis mine]:

3 The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue or a qualified-id. If the operand is a qualified-id naming a non-static or variant member m of some class C with type T, the result has type “pointer to member of class C of type T” and is a prvalue designating C::m. Otherwise, if the type of the expression is T, the result has type “pointer to T” and is a prvalue that is the address of the designated object (4.4) or a pointer to the designated function. [ Note: In particular, the address of an object of type “cv T” is “pointer to cv T”, with the same cv-qualification. — end note ] For purposes of pointer arithmetic (8.7) and comparison (8.9, 8.10), an object that is not an array element whose address is taken in this way is considered to belong to an array with one element of type T.


EDIT:

Below part is to justify the claim that address of operator & cannot return nullptr for a user defined type operand (that does not have overloaded & operator).

Below references are from C++17.

From 7.11 Pointer conversions

1 A null pointer constant is an integer literal (5.13.2) with value zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type. ....

From 8.10 Equality operators

2 If at least one of the operands is a pointer, pointer conversions (7.11), function pointer conversions (7.13), and qualification conversions (7.5) are performed on both operands to bring them to their composite pointer type (Clause 8). Comparing pointers is defined as follows:
(2.1) — If one pointer represents the address of a complete object, and another pointer represents the address one past the last element of a different complete object,88 the result of the comparison is unspecified.
(2.2) — Otherwise, if the pointers are both null, both point to the same function, or both represent the same address (6.9.2), they compare equal.
(2.3) — Otherwise, the pointers compare unequal.

   A *obj1 = nullptr;
   A obj2;

It is guaranteed that the expression obj1 == &obj2 will evaluate to false.

发布评论

评论列表(0)

  1. 暂无评论