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

前端 未结 6 1845
無奈伤痛
無奈伤痛 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 06:40

    What does the standard say about this? Is it always legal to "allocate" empty block of memory?

    Every object has a unique identity, i.e. a unique address, which implies a non-zero length (the actual amount of memory will be silently increased, if you ask for zero bytes).

    If you allocated more than one of these objects then you'd find they have different addresses.

提交回复
热议问题