C++ new int[0] -- 会分配内存吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1087042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ new int[0] -- will it allocate memory?
提问by
A simple test app:
一个简单的测试应用程序:
cout << new int[0] << endl;
outputs:
输出:
0x876c0b8
So it looks like it works. What does the standard say about this? Is it always legal to "allocate" empty block of memory?
所以看起来它有效。标准对此有何看法?“分配”空内存块总是合法的吗?
采纳答案by Faisal Vali
From 5.3.4/7
从 5.3.4/7
When the value of the expression in a direct-new-declarator is zero, the allocation function is called to allocate an array with no elements.
当 direct-new-declarator 中表达式的值为零时,将调用分配函数来分配一个没有元素的数组。
From 3.7.3.1/2
从 3.7.3.1/2
The effect of dereferencing a pointer returned as a request for zero size is undefined.
取消引用作为零大小请求返回的指针的效果是未定义的。
Also
还
Even if the size of the space requested [by new] is zero, the request can fail.
即使 [by new] 请求的空间大小为零,请求也可能失败。
That means you can do it, but you can not legally (in a well defined manner across all platforms) dereference the memory that you get - you can only pass it to array delete - and you should delete it.
这意味着您可以这样做,但您不能合法地(以明确定义的方式跨所有平台)取消引用您获得的内存 - 您只能将其传递给数组删除 - 您应该删除它。
Here is an interesting foot-note (i.e not a normative part of the standard, but included for expository purposes) attached to the sentence from 3.7.3.1/2
这是一个有趣的脚注(即不是标准的规范部分,但包括用于说明目的)附加到 3.7.3.1/2 的句子
[32. The intent is to have operator new() implementable by calling malloc() or calloc(), so the rules are substantially the same. C++ differs from C in requiring a zero request to return a non-null pointer.]
[32. 目的是通过调用 malloc() 或 calloc() 来实现 operator new(),因此规则基本相同。C++ 与 C 的不同之处在于要求零请求返回非空指针。]
回答by Faisal Vali
Yes, it is legal to allocate a zero-sized array like this. But you must also delete it.
是的,像这样分配零大小的数组是合法的。但你也必须删除它。
回答by ChrisW
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.
如果您分配了多个这些对象,那么您会发现它们具有不同的地址。
回答by Evan Teran
Yes it is completely legal to allocate a 0
sized block with new
. You simply can't do anything useful with it since there is no valid data for you to access. int[0] = 5;
is illegal.
是的,分配一个0
大小的块是完全合法的new
。您根本无法使用它做任何有用的事情,因为没有可供您访问的有效数据。int[0] = 5;
是非法的。
However, I believe that the standard allows for things like malloc(0)
to return NULL
.
但是,我相信该标准允许诸如malloc(0)
return 之类的事情NULL
。
You will still need to delete []
whatever pointer you get back from the allocation as well.
您仍然需要delete []
从分配中获得的任何指针。
回答by shuaihanhungry
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.)
奇怪的是,即使请求零字节,C++ 也要求 operator new 返回一个合法的指针。(要求这种听起来很奇怪的行为简化了语言中其他地方的事情。)
I found Effective C++ Third Editionsaid like this in "Item 51: Adhere to convention when writing new and delete".
我发现Effective C++ Third Edition在“第 51 条:编写 new 和 delete 时遵守约定”中是这样说的。
回答by Shi Jieming
I guarantee you that new int[0] costs you extra space since I have tested it.
我向你保证 new int[0] 会花费你额外的空间,因为我已经测试过了。
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].
第二个代码片段的内存使用量减去第一个代码片段的内存使用量就是用于大量 new int[0] 的内存。