[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

Global Variables

Hello,

I have a set of C++ files and am having problems with global variables. The main function is in dct.cpp, which uses two function implementation files dct_lib.cpp and cif_lib.cpp with header files dct_lib.h and cif_lib.h. I wish to define a global int array 'basis[8][8]'. The idea is at the start of the main function to call a function in one of the libraries to initialise it. Then most of the subsequent processing needs to read these values but will never alter them. I currently pass references to a local variable between functions. I dont use an OOP design methodology so no classes and inheritance. Where should a global variable be defined in this case? I tried defining it in dct.cpp but the library implementation files cant see it...
0
trican
Asked:
trican
  • 4
  • 2
  • 2
1 Solution
 
AlexFMCommented:
Variable should be defined in one of source files (c, cpp):

int basis[8][8];

and defined with external keyword in h-file:

external int basis[8][8];

Include this h-file to any source file where you want to use the variable. If h-file is included to the file where this variable is defined, it's OK:

external int basis[8][8];

int basis[8][8];
0
 
tricanAuthor Commented:
Thanks Alex,

That sorts it out for me (although I think you mean extern instead of external)

Thanks again.
0
 
SalteCommented:
I suggest that if you use a library that you do NOT define a global variable that the library should access.

Usually your source program is "the boss" and a library should never use any variable defined in your program unless you explicitely let it.

If you want your library to access an array, send that array as argument to a function in the library.

func(basis);

if basis is defined as

int basis[8][8];

then the function func (which may be in a library) has then access to the values, it can modify the array even if the parameter is defined as:

void func(int arr[8][8]);

If the parameter is defined as:

void func(const int arr[8][8]);

it can not modify the array and can only read the values from it.

I believe it is very bad design if you make a library just randomly access variables defined in your main program. It is possible to do so by declaring the array external in the header file etc but I will very strongly advice against it.

Another thing is that if the libraries are libraries created by other people they most likely do not use random global variables defined in your main program and so it won't help to declare them external. Unless the library has specified in the interface that it assumes the existence of such a variable it won't use it. If it does (which is a bad design) you must define the array as external in your program.

The usual way to give a library data is to give it as an argument to a function in that library, that is the best design and there's no reason to do it any other way. Well, the exception is if the library require it this way and you don't have the source of the library.

Alf
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
AlexFMCommented:
Indeed, extern.
0
 
tricanAuthor Commented:
Thanks for the comments Alf but I think I need to clear up what I'm asking about. My programs are currently set up exactly like you advise. I have

int basis[8][8];

defined in main() and I pass references to this variable as arguments to all of the functions that need it in my libraries. In actual fact there are two 8x8 arrays that I have defined in main() say

int basis[8][8];
int trbasis[8][8];

The issue is that the functions that are called in main() are implemented in cif_lib.cpp and dct_lib.cpp. These functions in turn call other smaller functions in the dct_lib.cpp libraries. The two arrays that I mention are used extensively in these libraries. One of the functions called by main() initialise them and then references are passed between the functions as you recommend. My idea is that maybe if I define these arrays as global to the libraries I wouldnt have to pass references between them. This would be completely transparent to main() although I do see how this is sloppy practice. Perhaps I should keep my code as it is!

Thanks again.


0
 
SalteCommented:
Especially if you have two you should use the parameters, a reference parameter is simply a pointer and as such is only a 32 bit quantity on most machines, it doesn't cost much to pass that around compared to referencing an external array.

If you do want to declare things external you do that by using the word 'extern' in front.

Alf
0
 
tricanAuthor Commented:
Thanks for the comments Alf but I think I need to clear up what I'm asking about. My programs are currently set up exactly like you advise. I have

int basis[8][8];

defined in main() and I pass references to this variable as arguments to all of the functions that need it in my libraries. In actual fact there are two 8x8 arrays that I have defined in main() say

int basis[8][8];
int trbasis[8][8];

The issue is that the functions that are called in main() are implemented in cif_lib.cpp and dct_lib.cpp. These functions in turn call other smaller functions in the dct_lib.cpp libraries. The two arrays that I mention are used extensively in these libraries. One of the functions called by main() initialise them and then references are passed between the functions as you recommend. My idea is that maybe if I define these arrays as global to the libraries I wouldnt have to pass references between them. This would be completely transparent to main() although I do see how this is sloppy practice. Perhaps I should keep my code as it is!

Thanks again.


0
 
tricanAuthor Commented:
Sorry about that repeat ;(

Thanks for your help!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now