C++重载new []查询:参数的大小是多少?

Man*_*hna 8 c++ overloading new-operator

我有像这样的重载运算符new []

void * human::operator new[] (unsigned long int count){
      cout << " calling new for array with size  = " << count <<  endl  ;
      void * temp = malloc(count) ;  
      return temp ; 
}
Run Code Online (Sandbox Code Playgroud)

现在打电话

human * h = new human[14] ;
Run Code Online (Sandbox Code Playgroud)

比方说sizeof(human) = 16,但计算它的打印是232,这是14*16 + sizeof(int*)= 224 + 8.

为什么要分配这个额外的空间?它在哪里记忆?因为当我打印*h或者h[0]我得到相同的结果时,所以它不在内存块的开头.它是否正确或我在这里遗漏了一些东西?

Kon*_*lph 6

分配的额外空间用于存储数组的大小以供内部使用(实际上,这样delete[]知道要删除多少).

立即存储在存储范围的开始,之前 &h.要看到这一点,只需看看temp你内心的价值operator new[].该值将与中的不同&h.