#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    69
    Rep Power
    27

    Dynamic Arrays Filled with Zeroes by Default?


    I am stepping through a small section of C++ code and noticed that after resizing a dynamic array of type double, the array is already filled with zeroes.

    i) The user is prompted to enter the desired size of an N x N array to create.
    ii) The array is created.
    iii) The array is resized to the appropriate size.

    Code:
    vector<double> vector1D; // One-dimensional vector of type double
    vector <vector<double>> A; // Two-dimensional array of type double
    
    . . .
    
    cin >> N;  //N, the size of the vector and N x N array is input by the user
    
    . . .
    
    vector1D.resize(N);
    A.resize(N);
    for (int i = 0; i < N; ++i) A[i].resize(N);
    vector1D is now a 1-D array of length N, and A is a 2-D array of size N x N.

    Normally, I would explicitly loop through the elements of this vector and array and initialize all the elements to 0, but they are already 0.

    My question: Is this non-explicit initialization part of the C++ standard? Or is this behaviour specific to my compiler (Visual Studio Express 2013)?

    Can I skip explicitly initializing dynamic vectors and arrays? I'd like to speed up my code and avoid duplicating something that is already done.
    Or is it still best practice to do so?

    All advice and information is appreciated.
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,824
    Rep Power
    509
    Note they use begin, end, and iterators.
    Originally Posted by http://www.cplusplus.com/reference/vector/vector/vector/
    Code:
    // constructing vectors
    #include <iostream>
    #include <vector>
    
    int main ()
    {
      // constructors used in the same order as described above:
      std::vector<int> first;                                // empty vector of ints
      std::vector<int> second (4,100);                       // four ints with value 100
      std::vector<int> third (second.begin(),second.end());  // iterating through second
      std::vector<int> fourth (third);                       // a copy of third
    
      // the iterator constructor can also be used to construct from arrays:
      int myints[] = {16,2,77,29};
      std::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
    
      std::cout << "The contents of fifth are:";
      for (std::vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
        std::cout << ' ' << *it;
      std::cout << '\n';
    
      return 0;
    }
    I would think the default constructor is 0. "In the case of Vector containers if you don't specify the initial value as a second argument to the resize method the default constructor is used."
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,782
    Rep Power
    4300
    Originally Posted by b49P23TIvg
    I would think the default constructor is 0. "In the case of Vector containers if you don't specify the initial value as a second argument to the resize method the default constructor is used."
    What if the default constructor for a particular compiler returns 1 instead of 0. I would create or resize with the value specified just to be portable:
    Code:
    std::vector<double> vector1D(N, 0.0); // Explicitly set the vector's elements to 0.0
    
    std::vector<double> vector1D;
    vector1D.resize(N, 0.0); // Resize and set the empty elements to 0.0
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo