Mail Archives: djgpp-workers/1996/06/08/10:02:59
/*
===============================================================================
Markus F.X.J. Oberhumer <markus DOT oberhumer AT jk DOT uni-linz DOT ac DOT at>
Subject: Strange malloc()/free() behaviour
To: djgpp-workers AT delorie DOT com
===============================================================================
When running this short program I get the following output:
> ...
> Your system allowed me to malloc 15351808 bytes.
> But now there's no free memory !
Any hints what's going on ?
(Everything works fine with Watcom C32 and emx+gcc).
P.S. is a snapshot of the current libc somewhere available ?
*/
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
// allocate as much memory as possible
unsigned long sysinfo_mallocSize(void)
{
void **ptr, *p;
int i;
size_t malloc_size;
unsigned long m;
unsigned long total;
ptr = (void **) calloc(256,sizeof(void *));
if (ptr == NULL)
return 0;
// compute malloc_size
m = 16*1024L;
while (m <= 1024L*1024L*1024L) // 1 gigabyte
{
if ((size_t)m != m)
break;
malloc_size = (size_t) m;
m <<= 1;
}
total = 0;
for (i = 0; i < 256 && malloc_size >= 1024; )
{
p = malloc(malloc_size);
#if 1
fprintf(stdout,"malloc %12lu p: %p\n",malloc_size,p);
#endif
if (p != NULL)
{
ptr[i++] = p;
total += malloc_size;
}
else
malloc_size >>= 1;
}
// free everything
while (i > 0)
free(ptr[--i]);
free(ptr);
return total;
}
int main()
{
unsigned long s;
void *p;
s = sysinfo_mallocSize();
printf("Your system allowed me to malloc %lu bytes.\n",s);
p = malloc(8192);
if (p == NULL)
printf("But now there's no free memory !\n");
return 0;
}
- Raw text -