Go down the line
- Why does my application crash if I do not use a new one?
The virtual table is corrupt.
The virtual table gets stuck right after the allocated memory. when you are a new class, the generated code will correctly configure vtable. However malloc will not initialize vtable correctly
To see the virtual table, run g ++ -fdump-class-hierarchy
Vtable for Derived Derived::_ZTV7Derived: 3u entries 0 (int (*)(...))0 8 (int (*)(...))(& _ZTI7Derived) 16 Derived::x Class Derived size=16 align=8 base size=12 base align=8 Derived (0x10209fc40) 0 vptr=((& Derived::_ZTV7Derived) + 16u) <-- notice how this is part of the structure Base (0x10209fcb0) 0 nearly-empty primary-for Derived (0x10209fc40)
For the same reason, without operator = overloading, the generated assembler code will only copy data, not vtable [again, the compiler only knows to copy data, not vtable]
If you want to see a pointer based version with a valid vtable function:
Derived e(123); d = &e;
- Should new ones also be used for non-polymorphic types?
If you use virtual functions, then yes, even for non-polymorphic types
- I hope that the alignment needed for my class is the class size returned by sizeof, so any address in the form address_returned_by_malloc + I * sizeof (my_class) is suitable for placing my objects.
Alignment is not a problem.
Foo bah
source share