According to the C++03 Standard (5.3.4/7):
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.
By my reading, this means that this code is legal and has a specified effect:
#include <iostream>
#include <string>
using namespace std;
class A
{
public:
A() : a_(++aa_) {};
int a_;
static int aa_;
};
int A::aa_ = 0;
int main()
{
A* a = new A[0];
// cout << "A" << a->a_ << endl; // <-- this would be undefined behavior
}
When I run this code under the debugger, I see that A
's constructor is never called. new
does not throw, and returns a non-null, apparently valid pointer. However, the value at a->a_
is uninitialized memory.
Questions:
- In the above code, what does
a
actually point to? - What does is mean to "allocate an array with no elements?"
- Of what practical use is it to allocate an array with zero elements?