Malcolm said:
The implemetation is allowed to modify a pointer passed to free(). Generally
this isn't done because it is simpler to implement free() as a normal
function.
Actually, it's *required* to implement free() as a 'normal' function.
From the C89 standard:
<Q>
4.10.3.2 The free function
Synopsis
#include <stdlib.h>
void free(void *ptr);
Description
The free function causes the space pointed to by ptr to be
deallocated, that is, made available for further allocation. If ptr
is a null pointer, no action occurs. Otherwise, if the argument does
not match a pointer earlier returned by the calloc , malloc , or
realloc function, or if the space has been deallocated by a call to
free or realloc , the behavior is undefined.
</Q>
The definition alone specifies (void *ptr), which is a clue that
the value of "ptr" will not be changed.
OTOH (to play devil's advocate here), the same document says
<Q>
4.10.3 Memory management functions
The order and contiguity of storage allocated by successive calls
to the calloc , malloc , and realloc functions is unspecified. The
pointer returned if the allocation succeeds is suitably aligned so
that it may be assigned to a pointer to any type of object and then
used to access such an object in the space allocated (until the space
is explicitly freed or reallocated). Each such allocation shall yield
a pointer to an object disjoint from any other object. The pointer
returned points to the start (lowest byte address) of the allocated
space. If the space cannot be allocated, a null pointer is returned.
If the size of the space requested is zero, the behavior is
implementation-defined; the value returned shall be either a null
pointer or a unique pointer. The value of a pointer that refers to
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
freed space is indeterminate.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
</Q>
Which would /imply/ that free() can change the value of "ptr".
Cheers,
Ron