问题
Consider
int main()
{
auto a = new int[0];
delete[] a; // So there's no memory leak
}
Between the copy initialisation and deletion, are you allowed to read the pointer at a + 1
?
Furthermore, does the language permit the compiler to set a
to nullptr
?
回答1:
Per recent CWG reflector discussion as a result of editorial issue 3178, new int[0]
produces what is currently called a "past-the-end" pointer value.
It follows that a
cannot be null, and a + 1
is undefined by [expr.add]/4.
来源:https://stackoverflow.com/questions/60442205/properties-of-a-pointer-to-a-zero-length-array