Re: [Dev-C++] Declaring global variables/memory mgmt?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2002-10-26 10:50:03
|
----MyHeader.h----
#define LEN_OF_SHELLNAME 50
#define MAX_SHELLS 4096
typedef struct {
...
} shell_t;
int read_shells();
void print_shells(const char* filename);
extern shell database[];
extern int shell_count;
-----------------
----MyMain.c----
#include <stdio.h>
#include <stdlib.h>
...
#include "MyHeader.h"
// All global variables for the program.
shell_t database[MAX_SHELLS];
int shell_count = 0;
int main(int argc,const char* argv[],const char* env[]) {
...
if (read_shells()) {
print_shells("a_filename.txt");
}
return 0;
}
---------------------
----MyUtils.c
#include <stdio.h>
#include <stdlib.h>
...
#include "MyHeader.h"
// Return 1 if ok
// Return 0 if error or terminated
int read_shells() {
...
return !terminated;
}
void print_shells(const char* filename) {
...
}
-----------------------
On Sat, 26 Oct 2002, Rob Peacock wrote:
> 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?
Be quite wary of the realloc() function. Yes it does work, but it is very,
very, very expensive to use. It might require quite a lot of time to
perform the request, and it might result in a very fragmented memory, i.e.
when it grows the size of an allocated memory block, it might have to move
the block somewhere else, while leaving a unused hole of memory at the old
location. Depending on other parts of the application, these holes might
never be reused.
A preferred solution is to allocate exactly the correct amount directly:
count = ask_for_shell_count();
shell_t *database = (shell_t*)malloc(count*sizeof(shell_t));
If count can't be known in advance, go for the vector of pointer
alternative.
// Will consume 16kB (4096*4) memory independent on size of shell_t
shell_t* database[MAX_SHELLS];
while (have_more_shells) {
if (shell_count >= MAX_SHELLS) {
complain_about_shell_count();
break;
}
database[shell_count] = malloc(sizeof(shell_t));
database[shell_count++] = read_one_shell();
}
>
> All comments are appreciated and thanks for all of your help!
>
> --->Rob
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: ApacheCon, November 18-21 in
> Las Vegas (supported by COMDEX), the only Apache event to be
> fully supported by the ASF. http://www.apachecon.com
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|