Thread: Globals in different source files

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    3

    Globals in different source files

    New to C so be kind please.

    I have a 16 bit dos program for a microcontroller that must use the old BC5.01 compiler. I recently ran into the error during compile saying too much global data in file. So I commented out some code so it compiles and while I have a known compiling source, I'm trying to move some stuff around and recompile.

    I have the following declared in my main.c

    Code:
    WORD  far *CustRegs;            
    WORD  far *rtvals ;           
    WORD  far *minvals;           
    WORD  far *maxvals;            
    WORD  far *avgvals;           
    WORD  far *frzvals;
    Then in main.c I initialize it:

    Code:
     if ((rtvals   = (WORD*)farcalloc(Maxregs,sizeof(WORD)))==NULL)
      { exit(1);}
      if ((minvals  = (WORD*)farcalloc(Maxregs,sizeof(WORD)))==NULL)
      { exit(1);}
      if ((maxvals  = (WORD*)farcalloc(Maxregs,sizeof(WORD)))==NULL)
      { exit(1);}
      if ((avgvals  = (WORD*)farcalloc(Maxregs,sizeof(WORD)))==NULL)
      { exit(1);}
      if ((frzvals  = (WORD*)farcalloc(Maxregs,sizeof(WORD)))==NULL)
      { exit(1);}

    Okay, first question, since these are already far, will I benefit in moving the declarations to it's own header and the init into it's own source?


    How is the best way to do this so main.c can still use all these arrays?

    Thanks,
    Richard

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is no "best". There is only a question of what you trade off to achieve what you intend.

    The pointers being far makes no difference to whether there is an advantage in reorganising your source (eg what goes in source files versus headers).

    What benefit are you seeking by reorganising your code?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    3
    First, I have to split my Main.c into multiple sources, so they are organized and also to reduce the compiler warning too much global data in file.

    In order to do that, multiple source files will need access to those arrays that I showed.

    So, how is the best way handle those arrays so they can be used in my other source files?

    Can I put the items below in their own header file like this

    Code:
    #ifndef MBRegs_H
    #define MBRegs_H 
    
    WORD  far *CustRegs;
    WORD  far *rtvals ;
    WORD  far *minvals;
    WORD  far *maxvals;
    WORD  far *avgvals;
    WORD  far *frzvals;
    
    void Init_MBRegs(void);
    
    #endif
    Then create a source.c file and put the Init_MBRegs in it like...

    Code:
    #include MBRegs.h
    
    void Init_MBRegs(void)
    {
     if ((rtvals   = (WORD*)farcalloc(Maxregs,sizeof(WORD)))==NULL)  { exit(1);} 
     if ((minvals  = (WORD*)farcalloc(Maxregs,sizeof(WORD)))==NULL)  { exit(1);}
      if ((maxvals  = (WORD*)farcalloc(Maxregs,sizeof(WORD)))==NULL)  { exit(1);}
      if ((avgvals  = (WORD*)farcalloc(Maxregs,sizeof(WORD)))==NULL)  { exit(1);}
      if ((frzvals  = (WORD*)farcalloc(Maxregs,sizeof(WORD)))==NULL)  { exit(1);}
    }
    Will I be able to access the data in the arrays from any unit that includes the MBRegs.h?

    Richard
    Last edited by rwskinner; 06-29-2012 at 01:10 PM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I'm not sure that moving globals into other files is going to prevent that problem. I would guess that the total amount of global data is a problem.

    Does your compiler generate a map file? And do you have a map file viewer?
    These show where all your global variables and static data etc go. it will help to show what is taking up the most space etc, and help show how you can rearrange things and change segment sizes etc.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > Will I be able to access the data in the arrays from any unit that includes the MBRegs.h?
    Yes, so long as the first thing main() does is call Init_MBRegs()

    The only thing you need to watch out for in the rest of the code is anywhere where you might have done sizeof(rtvals) (or any of the other arrays which are now just pointers).
    If you do, you need to fix the code.

    (PS - this is 20+ year old memory, most of the planet left DOS behind long ago)
    Which memory model are you compiling with?
    If you have compact or large, then you could just put each array definition in a separate source file, eg.
    Code:
    // rtvals.c
    WORD rtvals[Maxregs];
    Then your header file would be
    Code:
    extern WORD rtvals[];
    // ditto for all your other arrays
    In the large model for example, you're restricted to a max of 64K code/data within a single source file. But spreading things over multiple source files allows you to access multiple 64K segments in your whole program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    3
    Thanks!
    So I can initialize them all at the same time from main.c correct in InitMBRegs(), Or do they have to be initialized in their own c file?

    Richard


    Quote Originally Posted by Salem View Post
    > Will I be able to access the data in the arrays from any unit that includes the MBRegs.h?
    Yes, so long as the first thing main() does is call Init_MBRegs()

    The only thing you need to watch out for in the rest of the code is anywhere where you might have done sizeof(rtvals) (or any of the other arrays which are now just pointers).
    If you do, you need to fix the code.

    (PS - this is 20+ year old memory, most of the planet left DOS behind long ago)
    Which memory model are you compiling with?
    If you have compact or large, then you could just put each array definition in a separate source file, eg.
    Code:
    // rtvals.c
    WORD rtvals[Maxregs];
    Then your header file would be
    Code:
    extern WORD rtvals[];
    // ditto for all your other arrays
    In the large model for example, you're restricted to a max of 64K code/data within a single source file. But spreading things over multiple source files allows you to access multiple 64K segments in your whole program.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The point is, all other source files will have header "extern ..." either by explicit mention or via #include "header...". The ONE source where you want to initialize the values should not have "extern" in front of the variables.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple Source Files, make files, scope, include
    By thetinman in forum C++ Programming
    Replies: 13
    Last Post: 11-05-2008, 11:37 PM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. Makefiles, Object files, Source files, OH MY!
    By Inquirer in forum Linux Programming
    Replies: 2
    Last Post: 04-29-2003, 10:36 PM
  4. globals & multiple .c files
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 10-30-2002, 09:43 PM
  5. globals and splitting up your source
    By cppdude in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2002, 12:57 PM