为什么int数组没有初始化在C ++为零? [英] Why is int array not initialized to zeros in C++?

查看:322
本文介绍了为什么int数组没有初始化在C ++为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有C ++中的基本程序,其中列出的素数的给定数目。该做的工作的类是下面 - 我的问题是,当量输入为10(具体为10 - 它工作正常,我已经尝试了所有其他数字),生成略低于未初始化数组零的阵列。因此,数组的最后一个元素是空的返回false,我的code没有得到正常运行。

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;
}

我也尝试不(不抛出异常),具有相同的结果。在此先感谢您的帮助!

I also tried without (nothrow), with the same result. Thanks in advance for any help!

推荐答案

为int *质数=新(抛出异常)INT [金额]; 使用的默认 - 初始化的,其中像 INT 标量是一个空操作(即不执行任何实际的初始化)。

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 意思是:

To default-initialize an object of type T means:


      
  • 如果 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 for T 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, if T’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 (零) ,作为一个整型常量前pression,转换为 T

  •   
  • 如果 T 是一个(可能被cv修饰)非工会类类型,每个非静态数据成员和各基础类子对象是零初始化和填充初始化为零位;

  •   
  • 如果 T 是一个(可能被cv修饰)联合类型,对象的第一个非静态命名的数据成员是零初始化和填充被初始化为零位;

  •   
  • 如果 T 是一个数组类型,每个元素都是零初始化

  •   
  • 如果 T 是引用类型,没有初始化执行。

  •   
  • if T is a scalar type, the object is set to the value 0 (zero), taken as an integral constant expression, converted to T;
  • 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.

(所有重点煤矿。)

这篇关于为什么int数组没有初始化在C ++为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆