A simple test app:
cout << new int[0] << endl;
outputs:
0x876c0b8
So it looks like it works.
Yes it is completely legal to allocate a 0
sized block with new
. You simply can't do anything useful with it since there is no valid data for you to access. int[0] = 5;
is illegal.
However, I believe that the standard allows for things like malloc(0)
to return NULL
.
You will still need to delete []
whatever pointer you get back from the allocation as well.