delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/03/22:30:52

Date: Sat, 3 May 1997 22:08:46 -0400 (EDT)
From: Michael Phelps <morphine AT hops DOT cs DOT jhu DOT edu>
To: Matthew Bennett <Bennett AT btinternet DOT com>
cc: djgpp AT delorie DOT com
Subject: Re: Simple newbie problem - arrays
In-Reply-To: <01bc57a9$47894800$0b3e63c3@8652hvt73761>
Message-ID: <Pine.GSO.3.95.970503220625.9672A-100000@hops.cs.jhu.edu>
MIME-Version: 1.0

On 3 May 1997, Matthew Bennett wrote:

> Hi,
> 
> I'm new to djgpp, and even C/C++.  I'm not however new to programming ;)
> Anyway, one of the cool things about djgpp is it's protected mode stuff,
> allowing you to easily use extended memory.
> I decided to try this out with a simple array. Here's my program:
> 
> #include <stdio.h>     // I know I don't need this, but hey...
> 
> int main(void)
> {
> int q[100][100][100];
> q[80][80][80] = 44;
> printf("%d", q[80][80][80]);
> return 0;
> }
> 
> This array should only use about a meg, and so it should have plenty of
> space with extended mem etc.
> However, when I run it the program quits with an error - presumably because
> it ran out of memory.  It works fine for small arrays though.
> 
> How can I use large arrays like this - there must be a way in djgpp to
> store them using extended memory.

Yes, there is.  You're allocating a huge automatic array, and you're
asking for 100 * 100 * 100 * sizeof(int) = 4MB.  This is all placed on the
_stack_.  Consequently, you need to use the stubedit program to change the
stack size of the executable to be something greater than 4 megabytes
(the default is 512K).  Better solution:  use malloc() or calloc().


> 
> Any suggestions?
> 
> Thanks, 
> Matthew
> 


						---Michael Phelps
						   morphine AT cs DOT jhu DOT edu


                               CH3
                               |
                               N
                             / |
                     ______/   |
                    /      \   CH2
             _____/         \__|__      
           //     \\        /  |  \\     
         //        \\______/___CH2 \\  
          \        /       \       /
           \______/         \_____/
          / ------ \       /      \
        OH           \   /         OH
                       O
 
                   Morphine



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019