Sign in to follow this  

Is this okay?

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

The 2005 [url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf"]Working Draft, Standard for Programming Language C++[/url] states that

"When the value of the expression in a direct-new-declarator is zero, the allocation function is called to allocate an array with no elements."
[section 5.3.4]

So, according to the standard, yes it should be safe. But the usual rules apply, so be sure to delete[] what you new[].

Share this post


Link to post
Share on other sites
[quote name='Jethro_T' timestamp='1306785317' post='4817633']
Is it safe to allocate a zero size array? What about delete[]ing it?

A* a = new A[0];
delete[] a;


[/quote]


You can request storage of zero bytes, just like you described above. It will attempt to allocate an array with no elements.


Yes you can do it, yes they exist in common code, such as empty container classes, and yes they are useful.

When you have an array with no elements there is nothing for you to dereference, that is, you cannot access a[0] because it is beyond the length of the array. There may still be some uses for it, such as a placeholder or marker or an empty container.

Share this post


Link to post
Share on other sites

This topic is 2394 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.

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