Why is int array not initialized to zeros in C ++?

I have a basic C ++ program that lists a given number of primes. The class that does the work below - my question is that when you enter 10 for the "sum" (in particular 10 - it works fine for all the other numbers I tried), the array that is generated just below is not initialized array of zeros. Therefore, "the last element of the array is empty" returns false, and my code does not work correctly.

I don't know if I understood, but shouldn't initialize the int array with zeros? If not, what's so special about as many as 10 that make it initialize 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 your help!

+4
source share
2 answers

int* primes = new (nothrow) int[amount]; uses the default initialization, which for int scalars is noop (i.e., the actual initialization is not performed).

If you need explicit initialization, use value initialization instead:

 int* primes = new (nothrow) int[amount](); 

From the C ++ 11 standard, §8.5 / 6:

To initialize by default an object of type T means:

  • If T is a (possibly cv-qualified) class type, the default constructor for T is called (and initialization is poorly formed if T does not have an available default constructor);
  • if T is an array type, each element is initialized by default ;
  • otherwise, initialization fails .

If the program calls the default initialization of an object of type const-type T , T must be a class type with a user-supplied default constructor.

§8.5 / 7:

To initialize a value of an object of type T means:

  • If T is a class of a class (possibly cv-qualit) with a user-provided constructor, then the default constructor for T is called (and initialization is poorly formed if T does not have an available default constructor);
  • if T is a (possibly cv-qualified) non-unit class without a constructor provided by the user, then the object is initialized to zero and if T s the implicitly declared default constructor is nontrivial, this constructor is called.
  • if T is an array type, then each element is initialized with a value ;
  • otherwise, the object is initialized to zero .

An object initialized by initializing a value is considered constructed and, therefore, obeys the provisions of this International Standard that apply to “constructed” objects, objects “for which the constructor is completed,” etc., even if the constructor is not called to initialize the objects.

§8.5 / 6:

For zero initialization of an object or reference of type T means:

  • if T is a scalar type, the object is set to 0 (zero), taken as an integral constant expression converted to T ;
  • if T is a (possibly cv-qualified) non-unit class, each non-static data element and each sub-object of the base class are initialized with zeros, and padding is initialized with zero bits;
  • if T is a (possibly cv-qualified) join type, the objects of the first non-static named data element are initialized to zero and padding is initialized to zero bits;
  • if T is an array type, each element is initialized to zero ;
  • If T is a reference type, initialization is not performed.

And finally, from § 8.5 / 10:

An object whose initializer is an empty set of brackets, i.e. () must be initialized with a value.

(All my attention.)

+13
source

This is done only for efficiency. Not in all cases, arrays need to be pre-populated with a value, so C ++ does not do this by default.

If you use std::vector<int> instead of simple arrays (I recommend you), you have a constructor to set the initial value, which can be 0:

 std::vector<int> v(10,0); // 10 elements with 0 
+3
source

All Articles