Sign in to follow this  
Mantear

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

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this