开发者

C++ standard question

开发者 https://www.devze.com 2023-01-19 03:43 出处:网络
should the following result in undefined behavior? should value of pointer2 be NULL? double *pointer = 0;

should the following result in undefined behavior?

should value of pointer2 be NULL?

double *pointer = 0;
doubl开发者_StackOverflow中文版e &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. ]


Yes, you're dereferencing a null pointer when you do *pointer in line 2.

0

精彩评论

暂无评论...
验证码 换一张
取 消