C++ new int[0]——它会分配内存吗?

发布于 2024-07-27 01:27:01 字数 183 浏览 3 评论 0原文

一个简单的测试应用程序:

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

输出:

0x876c0b8

所以看起来它可以工作。 标准对此有何规定? “分配”空内存块总是合法的吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

彩扇题诗 2024-08-03 01:27:01

从5.3.4/7

当直接新声明符中表达式的值为零时,调用分配函数来分配一个没有元素的数组。

从3.7.3.1/2开始

取消引用作为零大小请求返回的指针的效果未定义。

即使[new]请求的空间大小为零,请求也可能失败。

这意味着您可以做到这一点,但您不能合法地(在所有平台上以明确定义的方式)取消引用您获得的内存 - 您只能将其传递给数组删除 - 并且您应该删除它。

这是一个有趣的脚注(即不是标准的规范部分,但出于解释目的而包含在内),附在 3.7.3.1/2 的句子中

[32。 目的是让 new() 操作符可以通过调用 malloc() 或 calloc() 来实现,因此规则基本相同。 C++ 与 C 的不同之处在于需要零请求才能返回非空指针。]

From 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.

From 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.

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

[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.]

一枫情书 2024-08-03 01:27:01

是的,像这样分配一个零大小的数组是合法的。 但您也必须将其删除。

Yes, it is legal to allocate a zero-sized array like this. But you must also delete it.

2024-08-03 01:27:01

标准对此有何规定? “分配”空内存块总是合法的吗?

每个对象都有一个唯一的标识,即唯一的地址,这意味着非零长度(如果您要求零字节,则实际的内存量将悄悄增加)。

如果您分配了多个这些对象,那么您会发现它们具有不同的地址。

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.

薆情海 2024-08-03 01:27:01

是的,使用 new 分配 0 大小的块是完全合法的。 您根本无法用它做任何有用的事情,因为没有可供您访问的有效数据。 int[0] = 5; 是非法的。

但是,我相信该标准允许诸如 malloc(0) 之类的东西返回 NULL

您仍然需要删除[]从分配中返回的任何指针。

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.

However, I believe that the standard allows for things like malloc(0) to return NULL.

You will still need to delete [] whatever pointer you get back from the allocation as well.

愚人国度 2024-08-03 01:27:01

我向你保证 new int[0] 会花费你额外的空间,因为我已经测试过它。

例如,
的内存使用量

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

明显小于

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

第二个代码段的内存使用量减去第一个代码段的内存使用量就是无数个new int[0]所使用的内存。

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].

深白境迁sunset 2024-08-03 01:27:01

奇怪的是,C++ 要求operator new 返回一个合法的指针
即使请求零字节。 (要求这个听起来奇怪的
行为简化了语言中其他地方的事情。)

我发现Effective C++ Third Edition在“Item 51:编写new和delete时遵守约定”中这样说。

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文