3 ms·
There is no such guarantee, a NULL reference can be created although it does require a little more effort than creating a null pointer. However, null is just a
by dbaupp 13y ago
There is no such guarantee, a NULL reference can be created although it does require a little more effort than creating a null pointer.
However, null is just a special case of "invalid" pointer (e.g. point to freed memory), and it's very easy to create an invalid reference, e.g. take a reference into a vector and then push a pile more elements onto it, causing the vector to reallocate and move in memory, invalidating the reference.
- lomnakkus 13y agoYou're talking about undefined behavior. There's no such thing as a null reference according to the C++ language specification.
- dbaupp 13y agoSo? It's still possible to create one. (There's no such thing as dereferencing an invalid pointer according to the standard, but that still happens.)
- lomnakkus 13y agoIf it takes undefined behavior to create a "null reference" then by definition it's not possible to create one and still have a well-defined program. That is, you have bigger problems than "null references" to worry about.