单个变量和数组的动态指针分配有什么区别?

What is difference between dynamic pointer allocation for a single variable and array?

C++中这三行有什么区别?

int *p= new int;
int *p= new int[10];
int *p = new int[];

我们已经将内存动态声明为指针变量p,为什么还要特别提到指针数组大小?

int *p = new int;

这为 int 类型的单个对象分配了足够的内存,并将指向它的指针存储在指向 int 变量的指针 p 中。这意味着 *p 引用了一个有效的 int 对象。

int *p = new int[10];

这为 int 类型的十个对象分配了足够的连续内存,并将指向第一个 int 的指针存储在指向 int 变量 p 的指针中。这意味着 p[0]p[9] 引用有效的 int 对象。

int *p = new int[];

此语句在语法上不正确。它不是有效的 C++,因此没有任何意义。

... why is it required to specifically mention pointer array size?

如果您不告诉内存分配器您需要多少 int 空间,内存分配器怎么知道要分配多少内存?