vendredi 26 décembre 2014

Why would I call new without storing the ptr?

Today I was reading though some source code and I encountered this



Pool<C> *pool = accomodate_component<C>();
new(pool->get(id.index())) C(std::forward<Args>(args) ...);


And this line confused me



new(pool->get(id.index())) C(std::forward<Args>(args) ...);


I didn't even think that it was legal C++. As it turns out pool->get(id.index()) returns a size which told me that it could be possible to tell C++ explicitly how much size it should allocate.


I looked it up and yes it is defined that way void* operator new ( std::size_t count );


Now I know that this is legal C++ but what I don't know is its purpose.


Why would I ever call new(size) Foo();. Shouldn't this be a memory leak? And isn't it completely pointless? If I don't have the ptr how I would ever access it?


Aucun commentaire:

Enregistrer un commentaire