Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
From: abowers@gate.net (Al Bowers)
Subject: Re: How do you release memory back to the kernel in C
Date: 1998/05/24	[thread overview]
Message-ID: <3568459a.8384137@news.gate.net> (raw)
In-Reply-To: Et9nr8.303@igsrsparc2.er.usgs.gov


On Wed, 20 May 1998 17:40:20 GMT, engebret@sg1.cr.usgs.gov (Chris
Engebretson) wrote:


>This generally works (well, better than a single bare free(), that
>is) but there are more robust ways to successfully free memory.
>Some Unices have a difficult time freeing large chunks of memory,
>and even the triple-free() approach above doesn't always work.
>
>What I generally recommend is freeing the block one byte at a time:
I don't see a smiley so I will make a few remarks.
>
>    void progressive_free(void *ptr, size_t size)
>    {
>	size_t i;
>
>	for(i = size - 1; i >= 1; --i)
Why size - 1, You make get unexpedted
results should the argument value of size
be 0..
>	    ptr = realloc(ptr, i);
>	free(ptr);
Why use free(). You can let realloc() do
all the work.

for(i = size; ;i--) {
   ptr = realloc(ptr,size);
   if(i== (size_t)0) break;
   }
>    }
>
>This gradually chops the block down to a single byte, which most
>implementations of free() handle quite nicely.  (Note the obvious
>safety defect in that the return of realloc() is not checked and
>the original pointer value is not preserved; sanity checking is
>left as an exercise to the reader.)
As long as size accurately represents the size of
the object, you can use this technique to 
almost totally verify that the pointer has 
been freed.

#include <stdio.h>
#include <stdlib.h>
  
#define ALLOC 10000
  
int FREE(void *p,size_t size);    
   
int main(void) {
   char *s = malloc(ALLOC);
   
   if(s == NULL) exit(EXIT_FAILURE);
   if(FREE(s,ALLOC)) {
      puts("Allocation most likely freed");
      s = NULL;
      }
   else {
      puts("Allocation was not freed");
      exit(EXIT_FAILURE);
      }
   return 0;
   }
   
int FREE(void *p,size_t size) {
   size_t i;
   void *temp;

   for(i = size ; ;--i) {
      temp = realloc(p,i);
      if(i == (size_t)0) return 1;
      if(temp == NULL) return 0;
      p = temp;
      }
   }    
Al Bowers                                
Tampa, FL
mailto: abowers@combase.com
http://www.gate.net/~abowers/index.html



  parent reply	other threads:[~1998-05-24  4:00 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-05-19  4:00 How do you release memory back to the kernel in C Asgar Ali
1998-05-19  4:00 ` Kaz Kylheku
1998-05-20  4:00   ` Chris Engebretson
1998-05-20  4:00     ` Serguei Patchkovskii
1998-05-20  4:00       ` Larry Weiss
1998-05-20  4:00       ` Kaz Kylheku
1998-05-20  4:00         ` Larry Weiss
     [not found]         ` <125D9A1BA03D8776.FEF9821DB00860BE.0E4 <895712304snz@genesis.demon.co.uk>
1998-05-21  4:00           ` Chris Engebretson
1998-05-21  4:00           ` Kaz Kylheku
1998-05-21  4:00             ` Larry Weiss
     [not found]           ` <2DECFB7C2125FF12.8D639AF581546583.E29C243161345CFE@library-proxy.airnews.net>
1998-05-22  4:00             ` CoolDoug
1998-05-22  4:00               ` Larry Weiss
1998-05-22  4:00               ` Kaz Kylheku
1998-05-25  4:00             ` Rolf Magnus
1998-05-25  4:00               ` Larry Weiss
1998-05-29  4:00               ` Patrick M.
1998-05-24  4:00     ` Al Bowers [this message]
1998-05-20  4:00   ` Larry Weiss
1998-05-21  4:00   ` Asgar Ali
1998-05-21  4:00     ` Kaz Kylheku
1998-05-23  4:00     ` Gerald A. Feldman
1998-05-19  4:00 ` firewind
1998-05-19  4:00 ` Lawrence Kirby
1998-05-21  4:00   ` John Hascall
1998-05-22  4:00     ` Asgar Ali
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox