简体   繁体   English

C ++指针数组初始化帮助

[英]C++ pointer array initialization help

I had trouble with initializing arrays of pointers. 我在初始化指针数组时遇到了麻烦。 What I found out compiling with gcc c++ (4.6.0) is: 我发现用gcc c ++(4.6.0)编译的是:

MyClass** a = new MyClass*[100];

Does not always initalize the array of pointers. 并不总是初始化指针数组。 (most of the time it did give me an array of null pointers which confused me) (大多数时候它确实给了我一个让我困惑的空指针数组)

MyClass** a = new MyClass*[100]();

DOES initialize all the pointers in the array to 0 (null pointer). DOES将数组中的所有指针初始化为0(空指针)。

The code I'm writing is meant to be be portable across Windows/Linux/Mac/BSD platforms. 我写的代码意味着可以在Windows / Linux / Mac / BSD平台上移植。 Is this a special feature of the gcc c++ compiler? 这是gcc c ++编译器的一个特殊功能吗? or is it standard C++? 还是标准的C ++? Where in the standard does it says so? 它在标准中的位置是这样说的吗?

This value-initialization is standard C++. 此值初始化是标准C ++。

The relevant standardeese is in C++98 and C++03 §5.3.4/15. 相关标准是C ++ 98和C ++03§5.3.4/ 15。 In C++98 it was default-initialization, in C++03 and later it's value initialization. 在C ++ 98中,它是默认初始化,在C ++ 03中,后来是值初始化。 For your pointers they both reduce to zero-initialization. 对于你的指针,它们都减少为零初始化。

C++03 §5.3.4/15: C ++03§5.3.4/ 15:

– If the new-initializer is of the form () , the item is value-initialized (8.5); - 如果new-initializer的格式为() ,则该项为值初始化(8.5);

In C++0x that paragraph instead refers to “the initialization rules of 8.5 for direct-initialization”, where in N3290 (the FDIS) you find about the same wording in §8.5/16. 在C ++ 0x中,该段代替引用“用于直接初始化的8.5的初始化规则”,其中在N3290(FDIS)中,您可以在§8.5/ 16中找到相同的措辞。

Cheers & hth., 干杯&hth。,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM