Hi all,

On Wed, 25 Apr 2001, Remco Schaar wrote:

> It is very hard to write perfect code,

True, but it's not hard to write code that doesn't leak memory.

void *p = NULL;
...
...
if( p ) { exit(POINTER_ERROR); }
void *p = malloc(n);
...
...
free( p );
p = NULL;
...
...

By which I mean that I use a certain set of pointers for memory
allocation and nothing else.  The pointers are declared and
initialized to NULL in the same statement.  Whenever I free some
memory I set the pointer which was used to point to it back to NULL.
Whenever I use a pointer for memory I first check that its value is
NULL.  Then if I try to use it when it's already being used for
something else my program tells me in a fairly unceremonious fashion.

This is one of a number of defensive techniques I've developed over
the years.  It's better to wrap this kind of stuff in a library of
defensive memory management routines (I expect you can buy one:) as
there's less room for errors and it saves a lot of typing.

Just my $0.02.

Geoff, I expect this will turn out to be one of those threads...

73,
Ged.

Reply via email to