delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/07/11:45:33

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: How big can I make my array?
Date: Sun, 07 Jun 1998 11:38:12 -0400
Organization: Two pounds of chaos and a pinch of salt.
Lines: 71
Message-ID: <357AB3E4.316B4ED2@cs.com>
References: <MPG DOT fe223cb1cc75732989717 AT news DOT virgin DOT net> <3578C43D DOT F4DDB90 AT cs DOT com> <MPG DOT fe3efebb95c6032989725 AT news DOT virgin DOT net>
NNTP-Posting-Host: ppp145.cs.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Andrew R. Gillett wrote:
> 
> > Your problem is stack overflow.  If you declare a local object of that
> > class, you're allocating all of the memory for the array from the
> > stack.  Since the default runtime stack for DJGPP programs is 256K,
> > you're overflowing it and your program will therefore crash most
> > spectacularly.
> 
> This structure is 1200K...

Whew!  Not only does this overflow the stack, but it also blows past the
data and code sections and tries to access outside the boundaries of
your program's address space.  Therefore, the instant you try to access
any of that memory, your program dies.

> > There are two solutions:  you can read chapter 15.9 of the DJGPP FAQ to
> > learn how to increase the stack size of your programs, or you can take
> > the better route and create your object statically or dynamically.
> 
> OK, sorry to keep asking questions like this, but:
> 
> How do I do that?
> 
> Do I have to use malloc or something to give myself a big chunk of
> memory? Or is it something else? The trouble is, I consider myself to be
> reasonably good at programming, but I don't have all that essential
> technical knowledge. Just the stuff they taught me at college, which is
> just the basics and nothing else...

Static allocation means making the object global (i.e., defined outside
of any function), or using the 'static' keyword when you create it as a
local object, e.g.:

void func( )
{
    static chunder_class foo;

    // ...
}

Dynamic allocation means that instead of an object, you create a
pointer-to-object and grab memory for it with malloc()/new.  Example:

void func( )
{
    chunder_class * pfoo;

    pfoo = new chunder_class;

    pfoo->pixel[x][y] = 255;

    // ...
}

When writing C++ code, the new operator should always be used instead of
malloc().  You must also remember to use the indirection operator ('->')
when dealing with pointers to objects.  See any good C or C++ reference
for a breakdown on the techniques of pointers and dynamic allocation.

REMEMBER ALWAYS:  Before you use a pointer variable, you must give it
something to point to!

Good luck!

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "Waking a person unnecessarily       |
|       aka Fighteer I       | should not be considered a capital   |
|   mailto:fighteer AT cs DOT com   | crime.  For a first offense, that    |
| http://www.cs.com/fighteer | is."            - Lazarus Long       |
---------------------------------------------------------------------

- Raw text -


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