www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1996/06/08/10:02:59

From: k3040e4 AT c210 DOT edvz DOT uni-linz DOT ac DOT at (Oberhumer Markus)
Message-Id: <199606081356.PAA01331@c210.edvz.uni-linz.ac.at>
Subject: Strange malloc()/free() behaviour
To: djgpp-workers AT delorie DOT com
Date: Sat, 8 Jun 1996 15:56:05 -0200 (MET DST)
Return-Read-To: markus DOT oberhumer AT jk DOT uni-linz DOT ac DOT at

/*
===============================================================================
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 -


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