www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1993/10/12/09:31:56

Date: Tue, 12 Oct 93 08:59:39 -0400
From: DJ Delorie <dj AT ctron DOT com>
To: HLI AT trisse DOT hj DOT se
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: Re: Malloc and free

>     I have made a small program which is supposed to alloc
>     and free memory. The problem is that it dosen't free
>     the allocated memory at all.

>     #include <stdio.h>
>     #include <malloc.h>

>     void main ()
>         {
>         int c;
>         void *p;

>         c=0;
>         while (c<800)
>             {
>             printf ("malloc: %d bytes \n",c*10000);
>             p=malloc (c*10000);
>             memset (p,0xff,c*10000);
>             free (p);
>             getch ();
>             c+=100;
>             }
>         }

>     I compile the program using: gcc prog.c -Wall

>     Why is'nt the memory free'd ?

How do you know it isn't freed?  There are no calls in there to check
memory status.  Note that since you allocate ever-increasing block
sizes, the smaller chunks are never available for future mallocs.  You
end up with a very large free list (djgpp's malloc doesn't merge free
blocks, sadly).

Try running your test the other way - start with big packets, then
allocate ever smaller ones.


- Raw text -


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