在GCC上我得到以下输出: custom_allocator :: allocate(00246C50,2234) custom_allocator :: deallocate(00246C50,2234) ) custom_allocator :: allocate(00247760,11178) custom_allocator :: deallocate(00247760,11174) 在MSVC 8上,我得到: custom_allocator :: allocate(00362850,2234) custom_allocator :: deallocate(00362850,2234) custom_allocator :: allocate(00366B68,11170) custom_allocator :: deallocate(00366B68,2234) 它们是否适合UB? WTF正在进行中?海湾合作委员会似乎是准确的,最低价格为...... b $!DAMN! 谢谢大家的时间。Keep in mind that I am a C programmer; well, anyway here is the C++program...__________________________________________________ ____________________#include <cstdio>#include <cstdlib>#include <new>struct custom_allocator {static void* allocate(std::size_t size)throw(std::bad_alloc()) {void* const mem = ::operator new(size);std::printf("custom_allocator::allocate(%p, %lu)\n",(void*)mem, (unsigned long)size);return mem;}static void deallocate(void* const mem, std::size_t size)throw() {std::printf("custom_allocator::deallocate(%p, %lu)\n",(void*)mem, (unsigned long)size);::operator delete(mem);}};template<typename T>struct allocator_base {static void* operator new(std::size_t size)throw(std::bad_alloc()) {return custom_allocator::allocate(size);}static void* operator new[](std::size_t size)throw(std::bad_alloc()) {return custom_allocator::allocate(size);}static void operator delete(void* mem)throw() {if (mem) {custom_allocator::deallocate(mem, sizeof(T));}}static void operator delete [](void* mem, std::size_t size)throw() {if (mem) {custom_allocator::deallocate(mem, size);}}};template<std::size_t T_size>class buf {char mem[T_size];};class buf2 : public buf<1234>, public allocator_base<buf2{char mem2[1000];};int main() {buf2* b = new buf2;delete b;b = new buf2[5];delete [] b;return 0;}__________________________________________________ ____________________On GCC I get the following output:custom_allocator::allocate(00246C50, 2234)custom_allocator::deallocate(00246C50, 2234)custom_allocator::allocate(00247760, 11174)custom_allocator::deallocate(00247760, 11174)On MSVC 8 I get:custom_allocator::allocate(00362850, 2234)custom_allocator::deallocate(00362850, 2234)custom_allocator::allocate(00366B68, 11170)custom_allocator::deallocate(00366B68, 2234)Are they both right due to UB? WTF is going on? GCC seems to be accurate atleast... DAMN!thank you all for your time.推荐答案" Chris M. Thomasson" < [email protected] in message news:2y ******************* @ newsfe06.iad ..."Chris M. Thomasson" <[email protected] in messagenews:2y*******************@newsfe06.iad... 请记住我是C程序员;好吧,无论如何这里是C ++ 计划...... __________________________________________________ ____________________Keep in mind that I am a C programmer; well, anyway here is the C++program...__________________________________________________ ____________________ [...][...] template< std :: size_t T_size> class buf { char mem [T_size]; };template<std::size_t T_size>class buf { char mem[T_size];}; 我将虚拟dtor添加到buf1,输出没有变化。I add virtual dtor to buf1, and no change in output. > class buf2:public buf< 1234> ;,public allocator_base< buf2 { char mem2 [1000]; };>class buf2 : public buf<1234>, public allocator_base<buf2{ char mem2[1000];}; > int main(){ buf2 * b =新buf2; 删除b; b =新buf2 [5]; 删除[] b; 返回0; } __________________________________________________ ____________________>int main() { buf2* b = new buf2; delete b; b = new buf2[5]; delete [] b; return 0;}__________________________________________________ ____________________ [...][...] Chris M. Thomasson写道:Chris M. Thomasson wrote: 请记住我是C程序员;好吧,无论如何这里是C ++ 程序... [..] 在GCC上我得到以下输出: custom_allocator :: allocate(00246C50,2234) custom_allocator :: deallocate(00246C50,2234) custom_allocator ::分配(00247760,11178) custom_allocator :: deallocate(00247760,11178) 在MSVC 8上我得到: custom_allocator :: allocate(00362850,2234) custom_allocator :: deallocate(00362850,2234) custom_allocator :: allocate(00366B68,11170) custom_allocator :: deallocate(00366B68,2234)Keep in mind that I am a C programmer; well, anyway here is the C++program...[..]On GCC I get the following output:custom_allocator::allocate(00246C50, 2234)custom_allocator::deallocate(00246C50, 2234)custom_allocator::allocate(00247760, 11174)custom_allocator::deallocate(00247760, 11174)On MSVC 8 I get:custom_allocator::allocate(00362850, 2234)custom_allocator::deallocate(00362850, 2234)custom_allocator::allocate(00366B68, 11170)custom_allocator::deallocate(00366B68, 2234) MSVC 9给出相同的输出,BTW。MSVC 9 gives the same output, BTW. 它们是否都归功于UB? WTF正在进行中? GCC似乎很准确 至少...... DAMN!Are they both right due to UB? WTF is going on? GCC seems to be accurateat least... DAMN! 嗯,运算符delete []的默认实现*不* 具有size论点。实际上有两个允许的声明 运算符delete []: void operator delete [](void * ptr)throw(); 和 void operator delete [](void * ptr,const std :: nothrow&)throw(); 我不知道还能告诉你什么。 V - 请在通过电子邮件回复时删除资本''A' 我没有回复最热门的回复,请不要问Well, the default implementation of the operator delete[] does *not*have the "size" argument. In fact there are two allowed declarations ofthe operator delete[]:void operator delete[](void* ptr) throw();andvoid operator delete[](void* ptr, const std::nothrow&) throw();I''m not sure what else to tell you.V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t ask" Victor Bazarov" < v。******** @ comAcast.netwrote in message news:gc ********** @ news.datemas.de ..."Victor Bazarov" <v.********@comAcast.netwrote in messagenews:gc**********@news.datemas.de... Chris M. Thomasson写道:Chris M. Thomasson wrote: >请记住我是C程序员;好吧,无论如何这里是C ++ 程序...... [...] 在GCC上我得到以下输出: custom_allocator :: allocate(00246C50,2234) custom_allocator :: deallocate(00246C50,2234) custom_allocator :: allocate(00247760,11178) custom_allocator :: deallocate(00247760,11178) 在MSVC 8上我得到: custom_allocator :: allocate(00362850,2234) custom_allocator :: deallocate(00362850,2234) custom_allocator :: allocate(00366B68,11170) custom_allocator :: deallocate(00366B68,2234)>Keep in mind that I am a C programmer; well, anyway here is the C++program...[..]On GCC I get the following output:custom_allocator::allocate(00246C50, 2234)custom_allocator::deallocate(00246C50, 2234)custom_allocator::allocate(00247760, 11174)custom_allocator::deallocate(00247760, 11174) On MSVC 8 I get:custom_allocator::allocate(00362850, 2234)custom_allocator::deallocate(00362850, 2234)custom_allocator::allocate(00366B68, 11170)custom_allocator::deallocate(00366B68, 2234) MSVC 9给出相同的输出,BTW。MSVC 9 gives the same output, BTW. > UB是否正确? WTF正在进行中? GCC似乎准确至少...... DAMN!>Are they both right due to UB? WTF is going on? GCC seems to be accurateat least... DAMN! 嗯,运算符delete []的默认实现确实*不* size论点。实际上有两个允许声明的 运算符delete []: void operator delete [](void * ptr)throw(); 和 void operator delete [](void * ptr,const std :: nothrow&)throw(); 我不知道还能告诉你什么。Well, the default implementation of the operator delete[] does *not* havethe "size" argument. In fact there are two allowed declarations of theoperator delete[]: void operator delete[](void* ptr) throw();and void operator delete[](void* ptr, const std::nothrow&) throw();I''m not sure what else to tell you. 这必须是GCC扩展吗?这很奇怪,好吧,也许不那么奇怪 因为它必须只是100%UB。那好吧。我最初以为我可以利用它获得的优惠。不是!!! ; ^ /This has to be GCC extension? This is weird, well, perhaps not so weirdbecause it simply MUST be 100% UB. Oh well. I initially thought I could takeadvantage of it; NOT!!!;^/ 这篇关于这个是UB ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-12 11:49