为什么 int 数组在 C++ 中未初始化为零?
我有一个 C++ 基本程序,它列出了给定数量的素数.完成工作的类如下 - 我的问题是,当数量"的输入为 10(特别是 10 - 它适用于我尝试过的所有其他数字)时,下面生成的数组未初始化为一个零数组.因此,数组的最后一个元素为空"返回 false,我的代码无法正常运行.
I have a basic program in C++ which lists a given number of primes. The class which does the work is below - my question is, when the input for "amount" is 10 (specifically 10 - it works fine for all other numbers I've tried), the array that is generated just below is not initialized to an array of zeros. Hence, "the last element of the array is empty" returns false, and my code does not get to run properly.
我不知道我是否理解错了,但是int数组不应该初始化为零吗?如果不是,整数 10 有什么特别之处导致它初始化为奇怪的值?
I don't know whether I've misunderstood, but shouldn't the int array initialize to zeros? If not, what is special about the integer 10 which causes it to initialize to strange values?
int* primecalc(int amount) {
int* primes = new (nothrow) int [amount];
//Throw an error if we can't allocated enough memory for the array.
if (primes==0) {
cout<< "Error allocating memory.";
return 0;
}
//Otherwise, start iterating through the numbers.
else {
primes[0] = 2;
primes[1] = 3;
int p = 2;
for (int i=4;primes[amount]==0;i++) {
int j = 0;
int k = 0;
while ((primes[j]<=floor(i/2)) && !(primes[j]==0) && (k==0)) {
if ((i % primes[j]) == 0) {
k=1;
}
j++;
} //end the while loop
if (k==0) {
primes[p] = i;
p++;
}
} //end the for loop
} //end the "else" part (this was only necessary in case memory could not be allocated)
return primes;
}
我也尝试不使用(nothrow),结果相同.提前感谢您的帮助!
I also tried without (nothrow), with the same result. Thanks in advance for any help!
推荐答案
int* primes = new (nothrow) int[amount];
is using default-initialization,这对于像 int
这样的标量是一个 noop(即不执行实际初始化).
int* primes = new (nothrow) int[amount];
is using default-initialization, which for scalars like int
is a noop (i.e. no actual initialization is performed).
如果您想要显式初始化,请改用 值初始化:
If you want explicit initialization, use value-initialization instead:
int* primes = new (nothrow) int[amount]();
<小时>
来自 C++11 标准,§8.5/6:
From the C++11 standard, §8.5/6:
默认初始化类型为T
的对象意味着:
- 如果
T
是(可能是 cv 限定的)类类型,则调用 T 的默认构造函数(如果 T 没有可访问的默认构造函数,则初始化是非良构的); - 如果
T
是数组类型,每个元素都是默认初始化的; - 否则,不会执行初始化.
- if
T
is a (possibly cv-qualified) class type, the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor); - if
T
is an array type, each element is default-initialized; - otherwise, no initialization is performed.
如果程序要求对 const 限定类型 T
的对象进行默认初始化,则 T
应是具有用户提供的默认构造函数的类类型.
If a program calls for the default initialization of an object of a const-qualified type T
, T
shall be a class type with a user-provided default constructor.
§8.5/7:
值初始化一个T
类型的对象意味着:
To value-initialize an object of type
T
means:
- 如果
T
是具有用户提供的构造函数的(可能是 cv 限定的)类类型,则调用T
的默认构造函数(并且初始化错误- 如果 T 没有可访问的默认构造函数,则形成); - 如果
T
是(可能是 cv 限定的)非联合类类型,没有用户提供的构造函数,则该对象为零初始化,如果T
的隐式声明的默认构造函数是不平凡的,该构造函数被调用. - 如果
T
是数组类型,那么每个元素都是值初始化的; - 否则,对象被零初始化.
- if
T
is a (possibly cv-qualified) class type with a user-provided constructor, then the default constructor forT
is called (and the initialization is ill-formed if T has no accessible default constructor); - if
T
is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, ifT
’s implicitly-declared default constructor is non-trivial, that constructor is called. - if
T
is an array type, then each element is value-initialized; - otherwise, the object is zero-initialized.
值初始化的对象被认为是已构造的,因此受适用于构造的"对象、构造函数已完成的对象"等的本国际标准规定的约束,即使没有调用构造函数用于对象的初始化.
An object that is value-initialized is deemed to be constructed and thus subject to provisions of this International Standard applying to "constructed" objects, objects "for which the constructor has completed," etc., even if no constructor is invoked for the object’s initialization.
§8.5/6:
零初始化T
类型的对象或引用意味着:
To zero-initialize an object or reference of type
T
means:
- 如果
T
为标量类型,则将对象设为0
的值(零),取整型常量表达式,转换为T
; - 如果
T
是(可能是 cv 限定的)非联合类类型,则每个非静态数据成员和每个基类子对象都被初始化为零并且填充被初始化为零位; - 如果
T
是(可能是 cv 限定的)联合类型,则对象的第一个非静态命名数据成员初始化为零,填充位初始化为零; - 如果
T
是一个数组类型,每个元素都是零初始化的; - 如果
T
是引用类型,则不执行初始化.
- if
T
is a scalar type, the object is set to the value0
(zero), taken as an integral constant expression, converted toT
; - if
T
is a (possibly cv-qualified) non-union class type, each non-static data member and each base-class subobject is zero-initialized and padding is initialized to zero bits; - if
T
is a (possibly cv-qualified) union type, the object’s first non-static named data member is zero-initialized and padding is initialized to zero bits; - if
T
is an array type, each element is zero-initialized; - if
T
is a reference type, no initialization is performed.
最后从 §8.5/10 开始:
And finally from §8.5/10:
初始化器为空括号集的对象,即 ()
,应进行值初始化.
An object whose initializer is an empty set of parentheses, i.e.,
()
, shall be value-initialized.
(所有重点都是我的.)
(All emphasis mine.)
相关文章