在 C++ 中初始化一个空数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9951678/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:25:47  来源:igfitidea点击:

Initializing an empty array in C++

c++arraysstruct

提问by Rameez Hussain

I am writing a program in C++ and I need to initialize an array of a struct object I have created. It looks something like this:

我正在用 C++ 编写程序,我需要初始化我创建的结构对象数组。它看起来像这样:

typedef struct {
float x;
float y;
} vec2;

And then I initialize an array like this:

然后我像这样初始化一个数组:

vec2 hotSpot[1000];

I thought when I initialized such an array, it would be completely empty, but when I print the value of sizeof(hotSpot), it says 8000!

我以为当我初始化这样一个数组时,它会完全为空,但是当我打印 sizeof(hotSpot) 的值时,它显示为 8000!

Am I going wrong somewhere, or have I misunderstood some concept? How do I make this array empty?

我是不是哪里出错了,还是我误解了某些概念?我如何使这个数组为空?

回答by Kerrek SB

Your array is not, and can never be "empty". It has, and will always have, 1000 elements. Thus is the nature of C++ arrays.

你的数组不是,也永远不能是“空的”。它有并将永远有 1000 个元素。这就是 C++ 数组的本质。

The problem is that those elements are not in any deterministic state, and indeed accessing any element that has not been initialized is undefined behaviour.

问题是这些元素不处于任何确定性状态,并且确实访问任何尚未初始化的元素是未定义的行为

More generally, objects in C++ can never be "empty". They always exist. If an object didn't exist, there wouldn't be an object. Some objects always exist in a definite state, while other objects (such as primitive types, or a named object after having been std::moved) can exist in an uninitialized or indeterminate state -- they still exist, but the only thing you're allowed to do with them is assigna value to them.

更一般地说,C++ 中的对象永远不会是“空的”。他们永远存在。如果一个对象不存在,就不会有一个对象。有些对象总是以确定的状态存在,而其他对象(例如原始类型,或被std::moved后的命名对象)可以以未初始化或不确定的状态存在——它们仍然存在,但你唯一可以这样做对它们做的是为它们分配一个值。

回答by josephthomas

Your hotSpotcan not be empty as you initialized it as an array of 1000. There for, there are 1000 elements.

hotSpot不能为空,因为您将其初始化为 1000 的数组。因此,有 1000 个元素。

When vec2 hotSpot[1000];happens, it places all 1000 values as an uninitialized variable.

vec2 hotSpot[1000];发生时,它会将所有1000个值作为一个未初始化的变量。

The elements in hotSpotare not valid as they have not been initialized. If you are looking to set them all to zero, you could use memsetto initialize them all to zero.

中的元素hotSpot无效,因为它们尚未初始化。如果您希望将它们全部设置为零,则可以使用memset将它们全部初始化为零。

For more information on arrays, please take a look at this reference.

有关数组的更多信息,请查看此参考资料

回答by Reggie

I am guessing that you want to initialize your 'empty' array this way:

我猜你想用这种方式初始化你的“空”数组:

vec2 hotSpot[]; // Defines an array of undefined length

But if you want to initialize it as 'empty', i.e. fill its entire content with zeros:

但是,如果您想将其初始化为“空”,即用零填充其整个内容:

vec2 hotSpot[1000]; // Defines an array of 1000 items in length
memset(hotSpot, 0, sizeof(hotSpot)); // Fill the array with zeros

回答by Morpheus

A float is just 4 bytes in your machine.When initialized,the hotSpot takes up 8000 totally.

一个浮点数在你的机器中只有 4 个字节。初始化时,热点总共占用 8000 个。

回答by tartar

they are empty data-wise, but memory allocated to be able to hold 1000 * 2 float * 4 bytes

它们在数据方面是空的,但分配的内存能够容纳 1000 * 2 个浮点数 * 4 个字节