On Wed, 25 Apr 2001, G.W. Haywood wrote:
> Hi all,
Hi again,
> 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;
> ...
> ...
Of course I did not refer to "hello world", but more complex program that
generate new code which is executed etc... I was not even thinking about
starting a discussion or flame war about code "correctness" :-)
> 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.
Ok, but this requires all programmers to write clean and _consequent_
code, with all proper checks and a well-thought design
and... and... and... Theory is great, but in practice, this seems to
happen only with small groups/projects and then even rarely (all
software-enginering literature will confirm this, sadly). Certainly with
continuous running programs (as apache/mod_perl), even the smallest
mistake or misplacement in freeing memory (or not-freeing on purpose) will
cause growth of memory consumption per request.
> 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.
Go, and tell those guys in redmond... please :-)))
> Geoff, I expect this will turn out to be one of those threads...
No intensions !-)
> 73,
> Ged.
Bye,
Remco