Sign in to follow this  

Is this okay?

Recommended Posts

The 2005 [url=""]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;


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

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