placement new + array +alignment

前端 未结 4 1146
鱼传尺愫
鱼传尺愫 2020-12-10 04:06
SomeObj* Buffer;
char* BufferPtr = MemoryManager::giveMeSomeBytes(resX*resY*sizeof(SomeObj));
Buffer = new(BufferPtr) SomeObj         


        
4条回答
  •  独厮守ぢ
    2020-12-10 04:41

    @Mat, This is actually a great question. When I've used placement new[], I've had trouble deleting the storage. Even if I call my own symmetrical placement delete[], the pointer address is not the same as was returned by my own placement new[]. This makes placement new[] completely useless, as you've suggested in the comments.

    The only solution I've found was suggested by Jonathan@: Instead of placement new[], use placement new (non-array) on each of the elements of the array. This is fine for me as I store the size myself. The problem is that I have to worry about pointer alignments for elements, which new[] is supposed to do for me.

提交回复
热议问题