简体   繁体   中英

C++ standard question

should the following result in undefined behavior?

should value of pointer2 be NULL?

double *pointer = 0;
double &value = *pointer;
double *pointer2 = &value;

Yes.

double *pointer = 0;    // init `pointer` to a NULL pointer value
double &value = *pointer; // dereference it

The standard specifically speaks to this situation - from 8.3.2/4 "References":

A reference shall be initialized to refer to a valid object or function. [Note: in particular, a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the “object” obtained by dereferencing a null pointer, which causes undefined behavior. As described in 9.6, a reference cannot be bound directly to a bit-field. ]

是的,当您在第2行中执行*pointer时,您将取消引用空指针。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM