C++: allocate block of T without calling constructor

前端 未结 3 682
野趣味
野趣味 2021-02-01 04:01

I don\'t want constructor called. I am using placement new.

I just want to allocate a block of T.

My standard approach is:

T* data = malloc(sizeo         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-02-01 04:48

    The return from malloc is aligned for any type, so that's not a problem.

    Generally in C++, ::operator new would be the preferred way. You might also consider using an Allocator, which gives some extra flexibility (like being able to switch allocators easily.

提交回复
热议问题