C++ new int[0] — will it allocate memory?

前端 未结 6 1923
無奈伤痛
無奈伤痛 2020-11-22 06:16

A simple test app:

cout << new int[0] << endl;

outputs:

0x876c0b8

So it looks like it works.

6条回答
  •  梦如初夏
    2020-11-22 07:03

    Curiously, C++ requires that operator new return a legitimate pointer even when zero bytes are requested. (Requiring this odd-sounding behavior simplifies things elsewhere in the language.)

    I found Effective C++ Third Edition said like this in "Item 51: Adhere to convention when writing new and delete".

提交回复
热议问题