www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/12/03/00:20:31

From: edkiser AT jaxnet DOT com (M. Edward Kiser)
Newsgroups: comp.os.msdos.djgpp
Subject: Minor problem with operator new[]
Date: Tue, 03 Dec 1996 01:38:33 GMT
Organization: Southeast Network Services, Inc.
Lines: 93
Message-ID: <5800sc$5jp@ns2.southeast.net>
Reply-To: edkiser AT jaxnet DOT com
NNTP-Posting-Host: ts10-010.southeast.net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

/*
Recently, just out of curiosity, I decided to see how much memory I
could allocate under various conditions. I wrote this program to find
out, based on the assumption that operator new[] would throw an
exception if it couldn't allocate the memory. But it doesn't throw the
exception. It prints a message

"Virtual memory exceeded in new"

and terminates the program. This is very un-ANSI-like!

I searched the DJGPP FAQ (v2.01) and the djgpp\info\gcc.* files and
couldn't find any hints on how to change this "bad" behavior. So...
here I am.

(To compile this, save it as memtest.cpp and use

  gcc memtest.cpp -o memtest.exe -fhandle-exceptions -lstdcxx -s

)
*/

#include <iomanip.h>

bool couldget(unsigned long size)
{ bool succeeded=true;
  cout << "Trying to allocate " << size << " bytes... ";
  cout.flush();
  try
  { char * p=new char[size];
    for (unsigned long i=0; i<size; i++) p[i]=0;
    delete[] p;
    cout << "succeeded." << endl;
  }
  catch (...)
  { succeeded=false;
    cout << "failed." << endl;
  }
  return succeeded;
};

int main(void)
{ unsigned long size=2;

  while(couldget(size)) size<<=1;

  /* program never gets this far */
  unsigned long max=size;
  unsigned long min=size>>1;
  unsigned long t=(min+max)/2;

  while (t!=min && t!=max)
  { if (couldget(t)) min=t;
    else max=t;
    t=(min+max)/2;
  }

  cout << "Maximum allocation: " << t << " bytes." << endl;
};

/*
Typical output (Windows 95, with 20 MB of physical RAM):
Trying to allocate 2 bytes... succeeded.
Trying to allocate 4 bytes... succeeded.
Trying to allocate 8 bytes... succeeded.
Trying to allocate 16 bytes... succeeded.
Trying to allocate 32 bytes... succeeded.
Trying to allocate 64 bytes... succeeded.
Trying to allocate 128 bytes... succeeded.
Trying to allocate 256 bytes... succeeded.
Trying to allocate 512 bytes... succeeded.
Trying to allocate 1024 bytes... succeeded.
Trying to allocate 2048 bytes... succeeded.
Trying to allocate 4096 bytes... succeeded.
Trying to allocate 8192 bytes... succeeded.
Trying to allocate 16384 bytes... succeeded.
Trying to allocate 32768 bytes... succeeded.
Trying to allocate 65536 bytes... succeeded.
Trying to allocate 131072 bytes... succeeded.
Trying to allocate 262144 bytes... succeeded.
Trying to allocate 524288 bytes... succeeded.
Trying to allocate 1048576 bytes... succeeded.
Trying to allocate 2097152 bytes... succeeded.
Trying to allocate 4194304 bytes... succeeded.
Trying to allocate 8388608 bytes... succeeded.
Trying to allocate 16777216 bytes... succeeded.
Trying to allocate 33554432 bytes... Virtual memory exceeded in `new'
*/
--------
Ed Kiser (edkiser AT jaxnet DOT com)
"The great epochs of our lives are at the points where
 we rebaptize our badness as the best in us." -- Nietzsche

- Raw text -


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