Splitting code into separate files

Hello

I've just started using c++ so I'm learning as I go and doing lots of web searches. I'm trying to split my code into separate files as it's gotten quite large and unwieldy. I am using global variables in my code, which I know is frowned upon, but I have some very large arrays which I don't see how I can pass between functions without running out of memory.

I've managed to get the split-up code working when compiled, but I get highlighted errors in the code because of variables not being defined in the included functions. Let me show you an example. My main code looks like this.

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;
int VeryLargeGlobalArray[100]; //define very large global array
#include "OutputArray.h" //include output array function

int main() {
    OutputArray(50);
}   


And my included file, OutputArray.h, is this:

1
2
3
4
5
6
#include <iostream>
void OutputArray(int LastIndex){
    for(int i=0; i<=LastIndex; i++){
        cout << i << ": " << VeryLargeGlobalArray[i] << "\n";
    }
}


In this separate header file "cout" is underlined as it doesn't recognise the command and "VeryLargeGlobalArray" is also underlined as undefined.

I'm sure I'm approaching this completely wrong. I probably shouldn't be using header files for this, but I couldn't get it to work with cpp files. I've tried web searches but I can't seem to find a clear answer. Can someone set me straight?

I'm on Windows 10 using Visual Studio Code, my compiler is MinGW-W64.

Apologies if this has been covered elsewhere, but I've been unable to find an explanation that makes sense to me with my current level of knowledge.

Many thanks.
Last edited on
About splitting: https://www.cplusplus.com/articles/Gw6AC542/


I have some very large arrays which I don't see how I can pass between functions without running out of memory.

False.

There is no implicit copy of array.
1
2
3
4
5
6
int func( double arr[] );

int main() {
  double data[99999999];
  func( data );
}

The func() does not allocate double arr[99999999] and copy data to it.
The int func( double arr[] ); is in fact an alternate syntax for writing int func( double *arr );

Yes, during function call a pointer is implicitly created from the address of array. It is like you had written:
1
2
3
4
5
int main() {
  double data[99999999];
  double *temp = data;
  func( temp );
}

Since function does not get array but pointer, the function does not know whether the pointer is to one object or to some part of array. You need to pass the size separately:
1
2
3
4
5
6
int func( double arr[], int size );

int main() {
  double data[99999999];
  func( data, 99999999 );
}

Note that modifying the "array" in called function does modify the caller's function:
1
2
3
4
5
6
7
8
int func( double arr[], int size ) {
  for ( int i=0; i<size; ++i ) arr[i] = 2*i;
}

int main() {
  double data[99999999];
  func( data, 99999999 ); // assert: data changes
}



Overall, one should avoid plain arrays. There are std::array and std::vector. They are objects, know their size and would be copied.
However, you can pass them by reference:
1
2
3
4
5
6
7
8
int func( std::vector<double> &arr ) {
  for ( int i=0; i<arr.size(); ++i ) arr[i] = 2*i;
}

int main() {
  std::vector<double> data( 99999999 );
  func( data ); // assert: no copy and data changes
}

Last edited on
Thanks for tyour reply keskiverto. I will try that out later on.

A follow on question:

Say I have 5 global variables and I call a function which updates them. If I shouldn't have global variables, I can make the 5 variables local and pass them to the function. But the called function will update its own local copies of them, not the ones defined in the calling routine. I return a variable from the function, but I can't return 5 of them. So how do I do this? Do I pass pointers to those variables to the function?
Pass by reference. See http://www.cplusplus.com/doc/tutorial/functions/
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

void compute( int &task ) {
  task = 42;
}

int main() {
  int answer {};
  std::cout << "Before: " << answer << '\n';
  compute( answer );
  std::cout << "After:  " << answer << '\n';
}
Topic archived. No new replies allowed.