4 ms·
Or the author could have added 'virtual' to the method declaration and discussed why that code now crashes due to a NULL dereference...
by sharkbot 14y ago
Or the author could have added 'virtual' to the method declaration and discussed why that code now crashes due to a NULL dereference...
- repsilat 14y agoIt's disappointing that this works so reliably, even with the simplest of examples. I would have thought that if the compiler could resolve the function call statically it would do away with the vtable lookup, but neither g++ nor Clang do so at any level of optimisation.
- klodolph 14y agoWell, you see, that would change the behavior of the program. In order for it to resolve the vtable statically, which it CAN do, it would have to know what kind of vtable the NULL cowPointer has. And the true answer is that the program is already wrong once it accesses the vtable. To put it another way, such an optimizer would transform incorrect programs into correct programs. If you write correct programs, the optimization is useless since it only transforms incorrect programs. If you write incorrect programs, the optimization is dangerous since it makes it harder for you to figure out that your program is wrong.
- repsilat 14y ago> In order for it to resolve the vtable statically, which it CAN do, it would have to know what kind of vtable the NULL cowPointer has. Yeah. In retrospect my point doesn't really make any sense at all. Not sure what I was thinking there.