>I have posted this recently in the group comp.lang.c,
>I need a algorithm to find the quickest way to establish the maximum amount
>of dynamic memory that can be allocated using C.
>Using DOS and the function MALLOC.
#include "stdio.h"
#include "alloc.h"
#include "limits.h"
size_t _CalcMaxAlloc(size_t size, size_t minimum)
{
void* p;
if (size < minimum)
return 0;
p = malloc(size);
if (p)
{
size += _CalcMaxAllocSize(size);
free(p);
return size;
}
else
return = _CalcMaxAllocSize(size / 2);
}
size_t CalcMaxAlloc(size_t minimum)
{
return _CalcMaxAlloc(UINT_MAX); // NOTE: normal DOS C program assumed!
}
int main()
{
printf("Available memory is: %d bytes", CalcMaxAlloc(1));
return 0;
}
This above algorithm recursively allocates blocks of memory until it
can no longer allocate them or has reached the mininum size to allocate
(this is what the minimum parameter is for, in my example I used 1). Of
course, all blocks are freed before CalcMaxAllocSize() returns.
Remember, with most implementations of malloc() with DOS compilers, you
can only allocate 64K at a time.
I simply wrote the algorithm out without compiling, or testing it. I
just wanted to suggest as idea. This code, because of it's recursion,
might use up a lot of stack space. However, it is possible to "unwind"
recursive functions into iterative loops. The difficulty with this code
tho is keeping track of the malloc()'d blocks. In the current code, they
are conveniently free()'d as each successful call to _CalcMaxAlloc()
returns, as the stack unwinds. To expand this function into a loop, you
either have to keep your own "stack" of allocated blocks or, perhaps, use
each allocated block to form a linked-list of blocks to free.
Alternatively, most compiler C run-time libaries contain a "get available
memory" function that does the correct thing for the target platform. Is
this not sufficient for you?
Regards,
-------------------------------------------------------------------------
Matt Arnold | | ||| | |||| | | | || ||
mar...@netcom.com | | ||| | |||| | | | || ||
Boston, MA | 0 | ||| | |||| | | | || ||
617.389.7384 (h) 617.576.2760 (w) | | ||| | |||| | | | || ||
C++, MIDI, Win32/95 developer | | ||| 4 3 1 0 8 3 || ||
-------------------------------------------------------------------------
> I need a algorithm to find the quickest way to establish the maximum amount
> of dynamic memory that can be allocated using C.
>
> Using DOS and the function MALLOC.
Most DOS compilers have a function which tells you how much free space is in
the heap (eg: memfree() in Borland C). I assume you know this, and are trying
to produce a more portable solution?
--
!------- p...@mround.bt.co.uk -------! GAT d?(d) H s:-- g?>+++ p?>! au--- a- w+
!Phil "Phlash" Ashby!+44-1473-642735! v*>! C++$ L+>+++ 3+ ULHS+++(++++)$ P- E-
!MLB G/35,BT Labs,Martlesham,England! N++ K- W++ M V->! -po+ Y+ t-@ 5+ !j R G?
!- http://www.mround.bt.co.uk/~paa -! tv>! b+ D+ B? e-- u+(-) h---- f* r+++ y*
I need a algorithm to find the quickest way to establish the maximum amount
of dynamic memory that can be allocated using C.
Using DOS and the function MALLOC.
any help would be appreciated.
Thanks.
Matt Arnold (mar...@netcom.com) wrote:
: for...@liverpool.ac.uk (Mr R. Forster) writes:
: >I have posted this recently in the group comp.lang.c,
: >I need a algorithm to find the quickest way to establish the maximum amount
: >of dynamic memory that can be allocated using C.
: >Using DOS and the function MALLOC.
A better way to do it would be:
unsigned long MemAvail()
// Assuming Borlandc but it should be available in other compilers
{
REGS r;
r.h.ah=0x48;
r.x.bx=0xffff; // Try and allocate 1MB or RAM - it will fail
int86( 0x21, &r, &r ); // Call DOS
return( r.x.bx ); // This call failed and it returns to max mem avail in BX
}
Hope this helps. It should be easier and faster to implement. Under
borlandc there is the commands coreleft and farcoreleft which does the same
type thing.
- --
_________________________________________________________________________
This is my views, and mine alone. I do not presume to speak for anybody
but me, myself and my shadow.
- -------------------------------------------------------------------------
Robert Sandilands - a nutty engineer and bug squisher.
Virus Analist : CSIR Virus Protection Services
Tel: +27-12-841-2106, Fax: +27-12-841-3307, Email: rob...@vps.cis.co.za
- -------------------------------------------------------------------------
-----BEGIN PGP SIGNATURE-----
Version: 2.6
iQCVAgUBMM/N8IWe5NcbXFk5AQGQ3gP+Pzl4yPO3ELrdH0kXGcY41x8pEhq9UahA
HBwjMtAbYh47lQsdNEFl3dU9dbna3EaT2AgqioWldHtYqwulMtIOHAnMWZjJIvzo
vCNhqj5h0Lx1iTXVicyHmpBEifaWuRX1iqNLaUUlTo0k9hIBDAHBh7XBl8xzBy7g
2BVBNce29W0=
=X3DF
-----END PGP SIGNATURE-----
On Mon, 11 Dec 1995, Mr R. Forster wrote:
> I have posted this recently in the group comp.lang.c,
>
> I need a algorithm to find the quickest way to establish the maximum amount
> of dynamic memory that can be allocated using C.
>
> Using DOS and the function MALLOC.
>
> any help would be appreciated.
>
> Thanks.
>
>
Just use coreleft(), or farcoreleft() if you are in the far mode.