global variables - size

This is a discussion on global variables - size within the C++ Programming forums, part of the General Programming Boards category; I have to declare 4 global unsigned integer arrays of 30000 elements each and the compiler says it's too much...I ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    14

    global variables - size

    I have to declare 4 global unsigned integer arrays of 30000 elements each and the compiler says it's too much...I know there are special compiler parameters. I don't know about dinamic memory allocation or stuff like this What can I do?
    Thanks a lot!

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    what compiler are you using? I know that when I was using MSVC++6.0 student edition it also did not let me declare large arrays, and there is really nothing much that you can do if you are using that or many other trial versions.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    If you are in a real mode compiler that is far too large.

    It all depends on how much memory is available. In real mode no program can be larger than 640KB and no program can access data outside of 640KB - 1024LB actually - according to the design specs of the CPU - but DOS controls the area between 640KB and 1024KB. As well you must use the correct memory mode in C for real mode. You cannot transfer more than 64KB of data at any one time.

    In DJGPP, however, 4 unsigned integer arrays of 30000 elements would be possible IF you have enough memory for that.

    The memory requirements for that in 32-bit protected mode are:

    unsigned integer=4 bytes.
    30000*4=120000 - bytes per 30000 element array
    4*120000=480000 bytes required for all 4 arrays.

    so in real mode you can see that 640KB or 655360 bytes - 480000=175360 or 171.25 KB.

    Your code could not be larger than 171.25 KB in real mode - to say nothing of the memory that your IDE needs in order to function properly.

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    14
    I'm using BC4.5

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    Then there is no way to store that much data.

    Perhaps you could break the data up or you could store it on disk and cache it to your program in smaller chunks.

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    14
    There were some contests where the memory limit was set to some MBytes...so what can I do using the compiler I have (Borland C 4.5)?

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    14
    It says Linker Error: Automatic data segment exceeds 64K...
    So 64K is the maximum an array can be?

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    You can either use XMS or EMS to gain access to memory above 1MB - note that the A20 line must be enabled.


    EMS is the easiest way: Here is a small ems unit in inline assembler that will allow you to work with EMS quite easily.

    The only function that I did not implement is to check whether or not EMS is there. It is a fairly simple task.

    From Ralf Brown's RBIL.
    Code:
    INT 67 - Microsoft EMM386.EXE v4.20+ - INSTALLATION CHECK
    AX = FFA5h
    Return: AX = 845Ah/84A5h if loaded 
                 BX:CX -> API entry point (see #03666)
    Notes:   this call is available even if EMM386 is not providing EMS
             the returned AX is 845Ah inside of MSWindows, 84A5h
             under bare DOS.
             if no other program has hooked INT 67, an alternate 
             installation check is to test for the string "MICROSOFT
             EXPANDED MEMORY MANAGER 386" at offset 14h in the 
             INT 67 handler's segment; the word immediately 
             preceding this string contains the offset of the API entry 
             point
    SeeAlso: AH=3Fh,AX=FFA5h/BX=4345h,INT 21/AX=4402h"EMM386.EXE"
    
    (Table 03666)
    Call EMM386.EXE API entry point with:
    	AH = 00h get memory manager's status
    	    Return: AH = status
    			bit 0: not active (OFF)
    			bit 1: in "Auto" mode
    	AH = 01h set memory manager's state
    	    AL = new state (00h ON, 01h OFF, 02h AUTO)
    	AH = 02h Weitek coprocessor support
    	    AL = subfunction
    		00h get Weitek support state
    		    Return: AL = status
    				bit 0: Weitek coprocessor is present
    				bit 1: Weitek support is enabled
    		01h turn on Weitek support
    		02h turn off Weitek support
         --- v4.20-4.41 only ---
    	AH = 03h Windows support???
    	    AL = subfunction (00h, 01h)
    	AH = 04h print copyright notice to standard output
    		 (using INT 21/AH=09h)
    	AH = 05h print available report
    		 (the one shown when running EMM386 from the DOS prompt)
    SeeAlso: #01513 at INT 21/AX=4402h/SF=02h,#02617 at INT 2F/AX=12FFh/BX=0106h

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    14
    I think that's too much for my present knowledge..but thanks anyway! I'll handle with less elements in an array.

    I wanted to solve a problem where the maximum lenght of the array was 30000 and you couldn't solve it reading bit by bit (element after element) - you had to know them all.. What would you do then? Does dinamic memory allocation help? I would learn it in this case...

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    Yes your data segment can only be 64KB in size. You could move to the huge memory model which allows huge pointers and such, but your best bet is to either get a protected mode compiler, find another way to store the data, use EMS or XMS, or move to DJGPP.

  11. #11
    Registered User
    Join Date
    Dec 2002
    Posts
    14
    I could use DJGPP (that's a compiler isn't it?) but in case of a contest the evaluators may not compile it with this...

  12. #12
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    Here is a tutorial on how to use EMS.

    And here is the code for testing to see if EMM386.EXE is present or not:

    Code:
    #include <dos.h>
    
    #define WINDOWS 0x845A
    #define DOS           0x84A5
    
    int CheckForEMS(void)
    {
      //In pure C
      //union REGS regs;
      //regs.x.ax=0xFFA5;
      //int86(0x67, & regs, & regs);
      //return regs.x.ax;
    
      //In inline asm
      asm 
      {
         mov     ax,0FFA5h
         int        67h
      }
    
      return _AX;       //Sort of redundant, but....
    }
    
    
    int main(void)
    {
      switch (CheckForEMS())
      {
        case DOS:break;  //We are in pure DOS with EMM386 installed
        case WINDOWS:break; //We are in Windows with EMM386 installed
        default: //EMM386.EXE not installed
      }
    
      ...
      return 0;
    }
    Last edited by VirtualAce; 12-30-2003 at 08:54 AM.

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    Originally posted by danielpaval
    ...but in case of a contest the evaluators may not compile it with this...
    Ask em what compiler/system they will be using to judge the contest.....important info for a programming contest.

    gg

  14. #14
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    You don't want to be creating such huge global variables anyway, as a general rule. In answer to your question, yes, it would help you to learn about dynamic memory allocation. It might take a little while to achieve this but it's something you will need to become familiar with anyway sooner or later - why not sooner

  15. #15
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Global Variables, include files and classes
    By sharpstones in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2005, 10:06 AM
  3. Replies: 1
    Last Post: 09-05-2004, 06:42 PM
  4. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 09:28 AM
  5. GLobal variables
    By fuh in forum C++ Programming
    Replies: 21
    Last Post: 01-01-2003, 02:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21