Jump to content
  • Advertisement
Sign in to follow this  
Mantear

C++: have to 'delete[]' after using 'new T[0]'?

This topic is 3280 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

Should be a simple question, but I haven't been able to find the answer. If you do
int* x = new int[0];
, 'x' is not NULL. Do you still need to 'delete[]' it, even though no useable memory was allocated?

Share this post


Link to post
Share on other sites
Advertisement
Been a long time since I used C++ so I really don't remember but my general rule of thumb is if I can't find something explicitly stating what should happen then it is by definition undefined behavior. In that case you just shouldn't do it.

[edit] SiCrane with the ninja.
It states here that the only legal and in fact <required> operation is to delete[] it.
http://stackoverflow.com/questions/1087042/c-new-int0-will-it-allocate-memory

Share this post


Link to post
Share on other sites
WHENEVER you use new like this :

dataType heap = new dataType; (i.e no brackets)

you accompany it by using delete heap;

and WHENEVER you use brackets with new like this :

dataType heap2 = new dataType[someNumber]; (i.e new with brackets)

you need to accompany new with delete [] heap2 after your done.

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!