?
Solved

Global Variables

Posted on 2003-03-31
8
Medium Priority
?
218 Views
Last Modified: 2010-04-02
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
Comment
Question by:trican
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 150 total points
ID: 8237545
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
 
LVL 1

Author Comment

by:trican
ID: 8237582
Thanks Alex,

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

Thanks again.
0
 
LVL 12

Expert Comment

by:Salte
ID: 8237593
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:AlexFM
ID: 8237611
Indeed, extern.
0
 
LVL 1

Author Comment

by:trican
ID: 8238185
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
 
LVL 12

Expert Comment

by:Salte
ID: 8238215
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
 
LVL 1

Author Comment

by:trican
ID: 8238279
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
 
LVL 1

Author Comment

by:trican
ID: 8238287
Sorry about that repeat ;(

Thanks for your help!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question