delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/07/09/01:31:20

From: j DOT aldrich6 AT genie DOT com
Message-Id: <199607090525.AA200249924@relay1.geis.com>
Date: Tue, 9 Jul 96 05:00:00 UTC 0000
To: a DOT appleyard AT fs2 DOT mt DOT umist DOT ac DOT uk, djgpp AT delorie DOT com
Mime-Version: 1.0
Subject: Re: What if I run out of store

Reply to message 5864967    from A DOT APPLEYARD AT F on 07/08/96  8:49AM


>  Excuse me if this query is rather faqqish, but, since in version 2 I am not
>allowed indefinite heap size:-

I can't think of any DPMI compiler that doesn't allow you indefinite
heap size - are you perhaps thinking of the stack?  Heap size is
limited only by the amount of free memory you have.

>  (1) In (a) version 1, (b) in version 2, (c) in C and C++ in general
>including non-Gnu compilers, what happens if I call `new <typename>', or
>malloc(), and there is not enough space available, what happens? I used to
>think that it merely returns 0, but after finding this function in
>c:\djgpp\libsrc\gcc\src\libgcc2.c_:-
>  void _default_new_handler(){write(2,MESSAGE,sizeof(MESSAGE)); _exit(-1);}
>  I no longer feel so certain.

I dunno about new under C++, but if malloc() is unable to allocate memory,
it _will_ return a NULL pointer.  There are alternate versions of malloc(),
however, that do different thingswhen unable to allocate memory (xmalloc()
comes immediately to mind).  But standard malloc(), calloc(), and realloc(),
do nothing except return NULL if unable to find space.

>  (2) How can I trap the error condition of the stack running out of space?

You can't.  This is one of the failings of working under DPMI.  You can use a
debugger to evaluate how much stack you are using and initialize _stklen
appropriately within your program, or you can use stubedit to increase your
program's stack.  I suppose it would be possible to run a sort of online
debugger within a function that would determine the addresses of the
function's stack frame, local variables, and the end of static storage and
do a computation to determine if the stack had been exceeded, but it
would be very tedious to write and debug, and slow down your program as
well.  For more information on stack size, read the FAQ, section 15.8.

John

- Raw text -


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