Using malloc instead of new, and calling the copy constructor when the object is created

后端 未结 3 1382
眼角桃花
眼角桃花 2021-01-13 18:56

I wanted to try out TBB\'s scalable_allocator, but was confused when I had to replace some of my code. This is how allocation is done with the allocator:

Som         


        
3条回答
  •  独厮守ぢ
    2021-01-13 19:58

    Use placement new

    #include 
    //...
    int main()
    {
            S* s = (S*) malloc(sizeof(S));
            s = new (s) S();//placement new
            //...
            s->~S();
            free(s);
    }
    

提交回复
热议问题