0

should the following result in undefined behavior?

should value of pointer2 be NULL?

double *pointer = 0;
double &value = *pointer;
double *pointer2 = &value;
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
Anycorn
  • 50,217
  • 42
  • 167
  • 261
  • Related: http://stackoverflow.com/questions/2474018/when-does-invoking-a-member-function-on-a-null-instance-result-in-undefined-behav – GManNickG Oct 04 '10 at 19:38
  • 1
    Which C standard do you think even allows `double &value`??? They are different languages with different standards and different feature sets. Arrggghhhh!!!!! – dmckee --- ex-moderator kitten Oct 04 '10 at 19:39
  • 2
    Yes, the behavior is undefined. However, if you try it, you may find the code compiles and executes without complaint or crash, since the invalid reference isn't used except to initialize another pointer. "Nothing" is another acceptable response to undefined behavior. – aschepler Oct 04 '10 at 19:40
  • 1
    The Standard's note says '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'. It would now be interesting to know why the committee in some issue discussion states that merely dereferencing a null pointer is intended to be valid, given that note says the exact opposite. It is not some badly worded normative wording, but a clearly worded note, which indeed exist for the whole purpose of explaining intent. – Johannes Schaub - litb Oct 04 '10 at 19:43
  • @Johannes: Because there's more than one committee member? It's a good thing that the Standard conservatively represents the union intersection of their combined thoughts… – Potatoswatter Oct 04 '10 at 19:46
  • @Johannes: I'm confused by: "the committee in some issue discussion states that merely dereferencing a null pointer is intended to be valid...". I'm not sure what you're refering to there. – Michael Burr Oct 04 '10 at 22:09
  • @Michael http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#232 – Johannes Schaub - litb Oct 05 '10 at 04:25

2 Answers2

6

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. ]

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • does it matter that I do not read/write to the memory location per se? – Anycorn Oct 04 '10 at 19:39
  • @aaa: Not as far as the standard is concerned. – sepp2k Oct 04 '10 at 19:42
  • You might not have a crash at that point because the compiler might not actually try to 'read' memory location 0. However, that's one of the unfortunate natures of undefined behavior - the compiler doesn't have to act like it's an error. Even so, setting a reference via a NULL pointer dereference is undefined behavior. – Michael Burr Oct 04 '10 at 19:42
3

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

sepp2k
  • 363,768
  • 54
  • 674
  • 675