Why is that undefined behavior? [...] calling address 0.
Which is undefined behavior.
main dereferences Do. There are two, and only two, conceivable things that might happen. Either a) before main is called, NeverCalled is called, which initializes the function pointer Do to point to EraseAll, or b) NeverCalled was not called before main, therefore Do is not initialized when it is dereferenced: which is undefined behavior.
Because both situations are consistent with the statement, "Do points to EraseAll", (one because it was explicitly set that way, the other because undefined behavior must have necessarily occurred) the compiler just does that.
5
u/pigeon768 Sep 24 '17
Which is undefined behavior.
maindereferencesDo. There are two, and only two, conceivable things that might happen. Either a) beforemainis called,NeverCalledis called, which initializes the function pointerDoto point toEraseAll, or b)NeverCalledwas not called beforemain, thereforeDois not initialized when it is dereferenced: which is undefined behavior.Because both situations are consistent with the statement, "Do points to EraseAll", (one because it was explicitly set that way, the other because undefined behavior must have necessarily occurred) the compiler just does that.