指针数组成员,是否已初始化?
如果我有一个
class A
{
private:
Widget* widgets[5];
};
是否保证所有指针都是NULL,或者我需要在构造函数中初始化它们?对于所有编译器都是如此吗?
谢谢。
If I have a
class A
{
private:
Widget* widgets[5];
};
Is it guaranteed that all pointers are NULL, or do I need to initialize them in the constructor? Is it true for all compilers?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除非您这样做,否则该数组不会初始化。标准不要求数组被初始化。
The array is not initialized unless you do it. The standard does not require the array to be initialized.
如果它位于堆栈上或使用默认的堆分配器,则不会对其进行初始化(尽管您可以编写自己的分配器来执行此操作)。
如果它是全局变量,则它用零填充。
对于所有符合标准的编译器都是如此。
It is not initialized if it is on the stack or using the default heap allocator (although you can write your own to do so).
If it is a global variable it is zero filled.
This is true for all conformant compilers.
这取决于平台以及您分配或声明
A
实例的方式。如果它位于堆栈或堆上,则需要显式初始化它。如果它使用放置 new 和将内存初始化为零的自定义分配器,或者您在文件范围声明一个实例,并且平台将空指针常量按位为零,则您不需要。否则,你应该。编辑:我想我应该说显而易见的事情,那就是“不要假设这种情况会发生”。
尽管实际上答案是“这取决于平台”。该标准仅告诉您显式初始化或在文件范围内初始化时会发生什么。否则,最容易假设您所处的环境将执行与您希望执行的操作完全相反的操作。
如果您确实需要了解(出于教育或优化目的),请查阅文档并找出该平台可以依赖的内容。
It depends on the platform and how you allocate or declare instances of
A
. If it's on the stack or heap, you need to explicitly initialize it. If it's with placement new and a custom allocator that initializes memory to zero or you declare an instance at file scope AND the platform has the null pointer constant be bitwise zero, you don't. Otherwise, you should.EDIT: I suppose I should have stated the obvious which was "don't assume that this happens".
Although in reality the answer is "it depends on the platform". The standard only tells you what happens when you initialize explicitly or at file scope. Otherwise, it is easiest to assume that you are in an environment that will do the exact opposite of what you want it to do.
And if you really need to know (for educational or optimizational purposes), consult the documentation and figure out what you can rely on for that platform.
一般情况下,数组不会被初始化。但是,请记住,类类型的对象的初始值不仅取决于类本身的定义方式(构造函数等),还可能取决于创建对象时使用的初始化 。
因此,在某些特定情况下,问题的答案可能取决于您在创建对象时提供的初始化程序以及编译器实现的 C++ 语言版本。
如果不提供初始值设定项,数组将包含垃圾。
如果提供
()
初始值设定项,在 C++98 中数组仍将包含垃圾。然而,在 C++03 中,对象将被值初始化,并且数组将包含空指针。此外,具有静态存储持续时间的对象在任何其他初始化发生之前始终为零初始化。因此,如果您定义具有静态存储持续时间的
A
类的对象,则该数组最初将包含空指针。In general case the array will not be initialized. However, keep in mind that the initial value of the object of class type depends not only on how the class itself is defined (constructor, etc), but might also depend on the initializer used when creating the object.
So, in some particular cases the answer to your question might depend on the initializer you supply when creating the object and on the version of C++ language your compiler implements.
If you supply no initializer, the array will contain garbage.
If supply the
()
initializer, in C++98 the array will still contain garbage. In C++03 however the object will be value-initialized and the array will contain null-pointersAlso, objects with static storage duration are always zero-initialized before any other initialization takes place. So, if you define an object of
A
class with static storage duration, the array will initially contain null-pointers.