
ИП Моисеенко А.А. (МааСофт/ООО МааСофтваре) |
|
|
// Ошибка использования класса с унаследованием от класса со своим аллокатором // error under Microsoft Visual Studio 2022 (ver. August 2025) // error under g++ (gcc version 12.2.0 (Debian 12.2.0-14+deb12u1)) // Posible output: // // sizeof(A16) = 16 // sizeof(B16) = 32 // malloc(): invalid size (unsorted) // Aborted // // // or (>= 9.09.2025) // // sizeof(A16) = 16 // sizeof(B16) = 32 // b16 == nullptr // #ifdef _WIN32 #include "ToolsLib\ToolsLib.h" #else #include "ToolsLib/ToolsLib.h" #endif class A16 { public: _qword a[2]; A16(_qword q1, _qword q2) { a[0] = q1; a[1] = q2; } ~A16() { } void printfA16() { __utf8_printf("A16: %D %D\n", a[0], a[1]); } ADD_ALLOCATOR(A16) }; DEF_ALLOCATOR(A16) class B16 : public A16 { public: _qword b[2]; B16(_qword q1, _qword q2, _qword q3, _qword q4) : A16(q1, q2) { b[0] = q3; b[1] = q4; } ~B16() { } void printfB16() { __utf8_printf("B16: %D %D\n", b[0], b[1]); } void printfAB16() { printfA16(); printfB16(); } //ADD_ALLOCATOR(B16) /* // the next operators have no default values, you can use the next: void* __cdecl operator new__(size_t) noexcept(noexcept_new) = default; void* __cdecl operator new__(size_t, const char*, int, sDbgTmp*) noexcept(noexcept_new) = default; constexpr void __cdecl operator delete(void* object) = default; constexpr void __cdecl operator delete(void* object, const char*, int, sDbgTmp*) = default; */ }; //DEF_ALLOCATOR(B16) int main(int argn, char * args[]) { CMaaSetConsoleUtf8AndLocale Utf8ConsoleAndLocale; { __utf8_printf("sizeof(A16) = %D\n", (_qword)sizeof(A16)); __utf8_printf("sizeof(B16) = %D\n", (_qword)sizeof(B16)); B16* b16 = new B16(1, 2, 3, 4); // oops!! sizeof(A16) bytes allocated by our A16 allocator if (b16) { b16->printfAB16(); delete b16; // oops!! sizeof(A16) bytes are free by our A16 allocator } else { __utf8_printf("b16 == nullptr\n"); } } return 0; } Рейтинг: Назад Наверх |
Пользователь
Новости [...] Архив новостей. Новое на сайте
Сейчас на сайте
Гостей: 16
Пользователей: 0 Роботов: 3 Всего пользователей: 17 |
Мы принимаем к оплате: | ![]() |