delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/08/28/17:30:49

From: califax AT wupperonline DOT de (Klaus Petzold)
Newsgroups: comp.os.msdos.djgpp
Subject: Strange results with own maxheapblock()-function
Date: Fri, 28 Aug 1998 21:17:34 GMT
Organization: Customer of SpaceNet GmbH
Message-ID: <35e71d68.66941@news.space.net>
NNTP-Posting-Host: line316.kdt.de
Lines: 41
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Hi,

I tried to write a function, which finds out the size of the largest
block I can allocate on the heap. On my system it gives back
33558524 bytes, but with "char* ptr = (char *)malloc(size);" it
is possible to allocate 67112956 bytes. Has anyone an idea why
maxheapblock() fails?

Klaus
 
#include <stdlib.h>
#include <iostream.h>

unsigned long maxheapblock()
{
   unsigned long adder   = 1024 * 1024;
   unsigned long memsize = 0;
   char *cptr;

   do
   {
      do
      {
         memsize += adder;

         cptr = (char *)malloc(memsize);
         free(cptr);
      } while (cptr);

      memsize -= adder;
      adder   /= 2;
   } while (adder);

   return(memsize);
}

int main()
{
   cout << "free memory: " << maxheapblock() << " byte(s)" << endl;
}

- Raw text -


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