0

In Java, I can declare global (within the scope of a file) arrays and then initialize them in the main function.

public static int[] arr;

public static void main(String[] args) {
    arr = new int[N]; // N is possibly given as i/o.
    ...
}

This allows me to have this array is a global variable. Can I do the same thing in C++?

I tried the following code block in C++:

#include <bits/stdc++.h>
using namespace std;

int N;
vector<int> cows(N);

int main() {
    cin >> N;
    
    for (auto& elem : cows) {
        elem = 2;
    }
    cout << cows.size() << '\n';
}

This failed and it told me that the size of my array is 0. However, when I move the initialization of the vector into main, like this:

#include <bits/stdc++.h>
using namespace std;

int N;

int main() {
    cin >> N;
    vector<int> cows(N);

    for (auto& elem : cows) {
        elem = 2;
    }
    cout << cows.size() << '\n';
}

Now, the program gives me that the size of the array is indeed N (and not 0). However, now this vector is only avaiable in main and isn't global. Can I make the vector cows global, like you can do in Java?

Alan Chung
  • 117
  • 1
  • 3
    `cows.resize(N);` – Eljay May 25 '23 at 16:11
  • `int N; std::vector cows(N);` are initialized immediately (before `main` is reached) (`N` with `0`, and so vector with 0 elements). – Jarod42 May 25 '23 at 16:13
  • Yes, you can declare a vector as a global variable in C++ just like in Java, but there are a few differences in the way it should be done. In C++, the size of the vector needs to be determined at runtime, so you cannot initialize it directly with a global variable that is input at runtime. – salma nidar May 25 '23 at 16:17
  • 4
    Don't start learning C++ with bad habits. [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice), [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – 273K May 25 '23 at 16:24
  • Java `arr = new int[N];` would be quite literal C++ `cows = std::vector(N);` – 273K May 25 '23 at 16:26
  • 1
    Having a global variable is not generally desired / considered a bad practice. Do you really need the vector to be global? Also `N` need not be global at all. You could do `vector cows;` then resize later perhaps with `cows.resize(N);` in your `int main()` and `N` a local variable in main. – drescherjm May 25 '23 at 16:29
  • Closest analog I can think of would be `vector cows;` and then in `main`, after `N` is known, `cows = vector(N);` The optimizer should make very short work of the copying this implies. – user4581301 May 25 '23 at 16:42

1 Answers1

3

The problem is that in your first example,both N as well as cows are global variables which means that N will be at least zero initialized(due to static initialization) which in turn means that cows is defined to be a vector with no elements(that is, a vector of size 0).

To solve this, you can use std::vector::resize to resize the vector after asking the user the size as shown below:

int N;  //at least zero initialized(static initialization)
vector<int> cows(N); //you can rewrite this as just vector<int> cows;

int main() {
    cin >> N;

    //resize the vector passing the input size 
    cows.resize(N);
    
    for (auto& elem : cows) {
        elem = 2;
    }
    cout << cows.size() << '\n';
}

OTOH the second case works because you directly create a vector of size N where N is the size given by the user unlike the first case where it was zero initialized.

Jason
  • 36,170
  • 5
  • 26
  • 60