Where to define a large number of objects

Hi,

I am trying to run a simulation with a large number of objects (mainly arrays and vectors). I am not sure where shall I define my objects: inside or outside of the main() function, like the following two structures:

(1) ---------------
1
2
3
4
5
6
7
8
//main.cpp
int main(){
array<double, 1000> a_1 = {};
array<double, 1000> a_2 = {};
......
func_1(a_1, a_2, ..., a_100);
return 0;
}


(2) ----------------
1
2
3
4
5
6
7
8
//main.cpp
array<double, 1000> a_1 = {};
array<double, 1000> a_2 = {};
......
int main(){
func_1(a_1, a_2, ..., a_100);
return 0;
}


I know there is a question about scope. But besides this question (which seems have no difference between these two structures here), is there any difference in terms of execution performance or security issue?

Thanks in advance.
In the second case, the arrays have external linkage: other .cpp files may access them (if they write extern std::array<double, 1000> a_1; etc).

In the first case, the compiler can reason that no function that doesn't take a_1 by reference can possibly access a_1 or its contents, so more aggressive optimizations can be used.
Also second case can lead to such errors:
obj\Debug\main.o:main.cpp:(.data+0x0): multiple definition of `x'
obj\Debug\second.o:second.cpp:(.data+0x0): first defined here
Could you please, Cubbi, explain a bit more about your opinion:
In the first case, the compiler can reason that no function that doesn't take a_1 by reference can possibly access a_1 or its contents, so more aggressive optimizations can be used.
?

Topic archived. No new replies allowed.