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

前端 未结 6 1966
無奈伤痛
無奈伤痛 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:02

    I guarantee you that new int[0] costs you extra space since I have tested it.

    For example, the memory usage of

    int **arr = new int*[1000000000];
    

    is significantly smaller than

    int **arr = new int*[1000000000];
    for(int i =0; i < 1000000000; i++) {
        arr[i]=new int[0];
    }
    

    The memory usage of the second code snippet minus that of the first code snippet is the memory used for the numerous new int[0].

提交回复
热议问题