This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Run-time error with virtual function w/o invocation


On May 31, 2012 6:20 PM, "Arthur Schwarz"? wrote:
>
> Thank you Jonathan;
>
> I ran the compiler from the shell (g++ -pedantic main.cpp) without any errors or

That doesn't turn warnings on.

> warnings and with the same results. I don't think that the method 'new' is in
> error.

Which part do you think is correct, the part where you use endl (which
calls flush) then flush again, the part where you don't allocate any
memory, or the part where you forget the return value?

One might be ok, one is pointless but harmless except to performance,
and one is undefined behaviour and is definitely responsible for the
segfault.

> The segmentation fault does not occur unless the virtual function, fnc,
> is uncommented.

You got lucky, it's still undefined behaviour.

> If 'fnc' is commented, 'new' executes correctly.

No, it's still broken.

The reason it appears to work is that you never access anything in the
class, as it has no members. Adding a vtable causes the constructor to
dereference the garbage pointer value you didn't return.

Try adding an int member to the base class, initializing it in the
base constructor, and printing that out. Chances are you'll get a
segfault, virtual function or not.


> I think that
> this might be a compiler bug, but before I waste anyone's time I thought I'd get
> validation that I'm not doing some dumb ol' thing.

I guarantee you haven't found a compiler bug.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]