Jump to content
  • Advertisement
Sign in to follow this  
initial_y

The use of operator "new" ?

This topic is 4491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I saw some code in a template class :
::new (&m_pData[nIndex]) TYPE ;
is this mean :
&m_pData[nIndex] = ::new TYPE ;

Share this post


Link to post
Share on other sites
Advertisement
No, it's not quite the same. It is the so-called placement new. Basically you provide the new operator with an already allocated memory block and tell it to call an objects constructor at that position.

That's useful for object pools. The container allocates the memory for 100 objects in advance (in one single block) without ever constructing an object. Now when you request a new object the container simply calls the placement new which constructs an object at a specific position.

Note that objects constructed this way may not be deleted by calling delete. They have to specifically be only destructed:

pObject->~CObject();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!