1

Does x.resize(1024) guarantee that a valarray x will be zeroed?
Does std::valarray<float> z(1024); guarantee that it will be filled with zeros as well?

Is this true for Mac, Windows, Linux? Is this true even for C++03 (I'm not using C++11)?

(All documentation that I've read seems to prove it true, but 1) the docs don't explicitely state what happens if 2nd parameter is omitted 2) I'm trying to debug an exotic bug, that is hard to reproduce, in which some arrays are not zeroed by default, I'm unable for now to post a minimal code showing this bug, but I'll try to add one once available)


Example code:

#include <valarray>
#include <complex>

int main()
{
  std::valarray<double> x;
  std::valarray<std::complex<double>> y;
  std::valarray<float> z(1024);  
  x.resize(1024);
  y.resize(1024);
  // are all x, y, z  filled with zeros?
}
plasmacel
  • 8,183
  • 7
  • 53
  • 101
Basj
  • 41,386
  • 99
  • 383
  • 673
  • "The first member function initializes elements with their default constructor." Seems pretty clear to me. Although, this is not actually very precise and MSDN isn't authoritative, but the general point being made is correct. Here's a better reference: http://en.cppreference.com/w/cpp/numeric/valarray/resize. (Though still not authoritative; only the standard is.) – GManNickG Oct 17 '16 at 23:28
  • From the docs you linked: "The first member function [without the second argument] initializes elements with their default constructor." – user2357112 Oct 17 '16 at 23:28
  • @GManNickG: "...initializes elements with their default constructor" is, of course, formally incorrect, especially in the context of the OP's case. `float` and `double` do not have any constructors. Even if we purely hypothetically allow existence of the "default constructor" in `float` or `double`, who said that it sets it to zero? – AnT stands with Russia Oct 17 '16 at 23:42
  • @AnT: Hence the reason I said it was not precise. :) – GManNickG Oct 17 '16 at 23:52
  • Why the downvotes? Isn't this question relevant, the fact of wanting to be sure that something (that may sound simple) is really true? – Basj Oct 18 '16 at 06:55

1 Answers1

3

The standard declaration of valarray<T>::resize is

void resize(size_t sz, T c = T());

where c is the value used for new elements (if any). If some implementation decides to implement it as two overloaded functions instead of a single one with a default argument (which is permitted), it has to preserve the standard behavior.

This means that yes, if resize creates new elements, they have to be value-initialized (zero-initialized in your case).

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • So does this mean that we are sure that it will be zero all the time, in all implementations (at least Windows and Mac OS) ? – Basj Oct 17 '16 at 23:32
  • @Basj: Yes, for scalar types it will be zero all the time, unless your implementation is broken. – AnT stands with Russia Oct 17 '16 at 23:32
  • `They have to be value-initialized...` : by myself? or do you mean they *are* automatically initialized to 0? – Basj Oct 17 '16 at 23:33
  • @Basj: *Value-initialization* is a standard terms in C++ terminology. It is a specific kind of initialization triggered by `()` initializer. What it does depends on the type of the object being initialized. For scalar types it boils down to zero-initialization. For class types it can be something completely different. – AnT stands with Russia Oct 17 '16 at 23:34
  • Thanks for answer btw. Last thing: I added this in question while you were answering probably : what about `std::valarray z(1024);`? Does this line automatically zero it? – Basj Oct 17 '16 at 23:34
  • @Basj: Yes, it does. It *value-initializes* the new elements. Which means that `float` elements will be set to zero. – AnT stands with Russia Oct 17 '16 at 23:36
  • Thank you. Can you add this in answer for future ref? is there a source showing `valarray` has same interface as `resize`? – Basj Oct 17 '16 at 23:37
  • @Basj: It is not exactly the same. The constructor is defined separately for one-parameter and two-parameter versions. `resize` is defined as a single function with a default argument. But in the end, the behavior in question is identical. – AnT stands with Russia Oct 17 '16 at 23:38