[Dev-C++] Declaring global variables/memory mgmt?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Rob P. <py...@ea...> - 2002-10-26 07:30:22
|
Hi all,
I have a couple of questions for you and they probably have simple answers.
I hope you can help.
Using your advice, I have created an array of structs, essentially building
a database, mostly of int's and a couple of char arrays. I also built in
some input testing so that I know the data coming in is what it is supposed
to be.
Since this is a fairly large file, and my instructor from years ago told me
to keep main() small and uncluttered by keeping 'functional modules' in
their own files, and then #include them in main(), I would like to do just
that. The problem I have is that the data in the array is unavailable to
main() and other similar functions. He also mentioned that only variables
that must be global should, and most don't have to be.
In perusing one of my manuals, it mentions that the variables should be
made global by defining them in main() and then declaring them in a header
file using 'extern' but it was kind of unclear on how to do this. Since
this is going to be something I am sure I will be doing in a couple other
places, I would like to get a clear understanding of this.
Here is the struct, array, and constants that I want to make global:
-------- file data_in.c -----
#define LEN_OF_SHELLNAME 50
#define MAX_SHELLS 4096
typedef struct
{
int ShellIndex;
int CueNumber;
int ShellSize;
float BreakDelay;
float ShellDuration;
char ShellName[LEN_OF_SHELLNAME];
float ElapsedTime;
} database ;
database ShellCatalog[MAX_SHELLS];
I want to be able to twiddle the data from within main() or from within
other functions that may be called from somewhere else.
My second question is that someone mentioned in a prior post that by using
malloc(), realloc(), and free(), I could reduce the memory requirements by
only allocating the minimum memory required by the above array, especially
if I don't use the max number of records (4096). I'm sure that by making it
more memory efficient, it will run a bit faster as well. How would I
implement this?
All comments are appreciated and thanks for all of your help!
--->Rob
|