News for hackers. Stuff that matters.
Re:Dereferencing NULL Pointer, without a Seg Fault by sharkbot on Tuesday July 10, 2012 @10:25PM (#4227406)
Or the author could have added 'virtual' to the method declaration and discussed why that code now crashes due to a NULL dereference...
Re:Dereferencing NULL Pointer, without a Seg Fault by repsilat on Tuesday July 10, 2012 @10:33PM (#4227435)
It'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.
Re:Dereferencing NULL Pointer, without a Seg Fault by klodolph on Wednesday July 11, 2012 @12:37AM (#4227743)
Well, 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.

Re:Dereferencing NULL Pointer, without a Seg Fault by repsilat on Wednesday July 11, 2012 @04:31AM (#4228381)
> 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.