How could I sensibly overload placement operator new?

后端 未结 9 2018
北恋
北恋 2020-12-05 23:49

C++ allows overloading operator new - both global and per-class - usual operator new, operator new[] used with new[] stat

9条回答
  •  粉色の甜心
    2020-12-06 00:20

    My primary usage is to create a large array of objects. Its performing much better and has less overhead to allocate the memory in a whole block, i.e. using VirtualAlloc from Win32 (when programming windows). Then you just pass a ptr within that block to each objects placement new such as:

    char *cp = new char[totalSize];
    
    for(i = 0; i < count; i++, cp += ObjSize)        
    {                                                        
        myClass *obj = new(cp) myClass;             
    }
    

提交回复
热议问题