C++ 如何在初始化列表中将指针数组设置为空值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2615071/
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++ How do you set an array of pointers to null in an initialiser list like way?
提问by user233320
I am aware you cannot use an initialiser list for an array. However I have heard of ways that you can set an array of pointers to NULL in a way that is similar to an initialiser list.
我知道您不能为数组使用初始化列表。但是,我听说过一些方法可以以类似于初始化列表的方式将指针数组设置为 NULL。
I am not certain how this is done. I have heard that a pointer is set to NULL by default, though I do not know if this is guaranteed/ in the C++ standard. I am also not sure if initialising through the new operator compared to normal allocation can make a difference too.
我不确定这是如何完成的。我听说一个指针默认设置为 NULL,但我不知道这是否在 C++ 标准中得到保证。我也不确定与正常分配相比,通过 new 运算符初始化是否也会有所不同。
Edit: I mean to do this in a header file/constructor initialisation list. I do not want to put it in the constructor, and I do not want to use a Vector.
编辑:我的意思是在头文件/构造函数初始化列表中执行此操作。我不想将它放在构造函数中,也不想使用 Vector。
回答by AnT
In order to set an array of pointers to nulls in constructor initializer list, you can use the ()
initializer
为了在构造函数初始值设定项列表中设置指向空值的指针数组,您可以使用()
初始值设定项
struct S {
int *a[100];
S() : a() {
// `a` contains null pointers
}
};
Unfortunately, in the current version of the language the ()
initializer is the only initializer that you can use with an array member in the constructor initializer list. But apparently this is what you need in your case.
不幸的是,在当前版本的语言中,()
初始化器是唯一可以与构造函数初始化器列表中的数组成员一起使用的初始化器。但显然,这就是您的情况所需要的。
The ()
has the same effect on arrays allocated with new[]
在()
对与分配的数组一样的效果new[]
int **a = new int*[100]();
// `a[i]` contains null pointers
In other contexts you can use the {}
aggregate initializer to achieve the same effect
在其他上下文中,您可以使用{}
聚合初始值设定项来实现相同的效果
int *a[100] = {};
// `a` contains null pointers
Note that there's absolutely no need to squeeze a 0
or a NULL
between the {}
. The empty pair of {}
will do just fine.
请注意,但绝对没有必要挤0
或NULL
之间{}
。空对{}
就可以了。
回答by Vlad
You can switch from array to std::vector
and use
您可以从数组切换到std::vector
并使用
std::vector<T*> v(SIZE);
The values will be initialized by NULL
s automatically. This is the preferred C++ way.
这些值将由NULL
s 自动初始化。这是首选的 C++ 方式。
Update: Since C++11, there is one more way: using
更新:从 C++11 开始,还有一种方法:使用
std::array<T*, SIZE> array = {};
This behaves more like a corrected version of C-style array (in particular, avoids dynamic allocations), carries its size around and doesn't decay to a pointer. The size, however, needs to be known at compile time.
这更像是 C 风格数组的修正版本(特别是避免动态分配),携带其大小并且不会衰减为指针。但是,需要在编译时知道大小。
回答by Paul R
Normally an array will not be initialised by default, but if you initialise one or more elements explicitly then any remaining elements will be automatically initialised to 0. Since 0 and NULL
are equivalent you can therefore initialise an array of pointers to NULL
like this:
通常,默认情况下不会初始化数组,但是如果您显式初始化一个或多个元素,则任何剩余元素都将自动初始化为 0。由于 0NULL
是等效的,因此您可以NULL
像这样初始化指针数组:
float * foo[42] = { NULL }; // init array of pointers to NULL
回答by Billy ONeal
I am not certain how this is done. I have heard that a pointer is set to NULL by default, though I do not know if this is guaranteed/ in the C++ standard.
It is not guaranteed by the C++ standard. Built in types ( like pointers ) are filled with garbage unless set otherwise.
我不确定这是如何完成的。我听说一个指针默认设置为 NULL,但我不知道这是否在 C++ 标准中得到保证。
C++ 标准不保证它。除非另有设置,否则内置类型(如指针)会被垃圾填满。
I am also not sure if initialising through the new operator compared to normal allocation can make a difference too.
What do you mean by "normal allocation" ? If you're talking about an automatic variable, then you can do this:
我也不确定与正常分配相比,通过 new 运算符初始化是否也会有所不同。
“正常分配”是什么意思?如果你在谈论一个自动变量,那么你可以这样做:
MyType * pointers[2] = {}
MyType * pointers[2] = {}
and the pointers should be initialized to NULL.
并且指针应该初始化为NULL。
回答by Johan Kotlinski
void* p[10] = { 0 };
回答by wilhelmtell
If you have a member array then there is no way to initialize, unless it's a static member. If the array isn't a static member then you'll have to fill it inside the constructor's body.
如果您有成员数组,则无法初始化,除非它是静态成员。如果数组不是静态成员,则必须将其填充到构造函数的主体中。
That said, chances are you're really better off using a std::vector
. Other than for technical reasons such as unavailability of a standard STL for your platform, or the slightly lesser performance a std::vector
is better than an array by any and all criteria. If performance is the issue then make sure you profiled and know by numbers that it is an issue.
也就是说,使用std::vector
. 除了技术原因,例如您的平台没有标准 STL,或者性能稍差,astd::vector
比任何和所有标准都好。如果性能是问题,那么请确保您分析并通过数字知道这是一个问题。