Link to home
Start Free TrialLog in
Avatar of trican
trican

asked on

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...
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trican
trican

ASKER

Thanks Alex,

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

Thanks again.
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
Indeed, extern.
Avatar of trican

ASKER

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.


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
Avatar of trican

ASKER

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.


Avatar of trican

ASKER

Sorry about that repeat ;(

Thanks for your help!