delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/07/29/11:43:11

From: j DOT aldrich6 AT genie DOT com
Message-Id: <199607291539.AA198454776@relay1.geis.com>
Date: Sun, 28 Jul 96 22:12:00 UTC 0000
To: dbarrett AT engin DOT umich DOT edu
Cc: djgpp AT delorie DOT com
Mime-Version: 1.0
Subject: Re: Flat Memory Questions

Reply to message 0702211    from DBARRETT AT ENGI on 07/28/96 10:58AM


>Wow, thanks a lot.  I to malloc a meg and it worked just fine.  But, when
>I tried to defind a static array (ie- char Array[1024 * 1024]) I got a
>wierd runtime error
>
>I tried to allocate the array on the first line of the main function.
>The rest of the program is simply printf statements (except for a line
>that sets one of the array values to 5, but it never gets that far).  Is
>there something wrong with allocating a buffer this way?

Whoops!  I forgot to mention something about that:  the default
DJGPP runtime stack is only 256K, so you can't allocate more
automatic variable space than that without "spilling over" into
other parts of your program.  Actually, since many other things
use the stack as well, allocating any large automatic structure
is a bad idea.

The FAQ mentions this problem in section 15.9 and recommends
that you either set the variable _stklen in your program to a
larger value, or use the 'stubedit' program to increase the
stack of the stubbed executable.  (Note:  the paragraph in the
FAQ which states that the previously allocated stack space is
discarded is a holdout from v1.* and is incorrect.)

Simply increasing the stack, however, is not really the best way
around the problem, because no matter how large the stack is,
there's always the chance that recursive calls or deep function
nesting could still overflow it.  For example, consider:

void munch_stack( int times )
{
    char bloated[256 * 1024];

    if ( times == 0 )
        return;
    else
        munch_stack( --times );

    return;
}

This little function could eat up every bit of stack space you give it and
still be hungry for more.  :)  In addition, consider that when you're running
under a debugger like gdb or fsdb, your program uses the debugger's
stack instead of its own.

The simplest and safest way to allocate large amounts of space is
to do it statically or dynamically, as Sengan mentioned.  Not only
are you guaranteed success as long as you have enough available
memory, you don't tear up the guts of your computer if you don't.  :)

John

- Raw text -


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