Re: A debugging implementation of malloc



Eric Sosman <esosman@xxxxxxxxxxxxxxxxxxx> writes:
jacob navia wrote:
In the C tutorial for lcc-win32, I have a small chapter
about a debugging implementation of malloc.
[...]

Others have commented on the alignment issues, and on
the wisdom of initializing newly-allocated memory to a
"useful" value like all zeroes. I've got a few further
suggestions:

When an allocation is released, consider filling it
with garbage. This may help catch erroneous uses of an
area after it's been freed, as in the famously incorrect
code for freeing a linked list:

for (ptr = head; ptr != NULL; ptr = ptr->next)
free(ptr);
[...]

Better yet: when allocating memory, fill it with one kind of garbage
(to detect, though not reliably, the error of accessing uninitialized
memory), and when releasing it, fill it with another kind of garbage
(to detect, though not reliably, the distinct error of attempting to
access freed memory). It could be a substantial performance hit, but
it might be worth it if it detects errors.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: Tracking down a garbage collection problem
    ... but I don't know what methods are available to the Ruby ... megabytes of memory; if I process many files in a single run then ... I would expect garbage collection to kick in along the way but it ... practice of software engineering, and what separates software ...
    (comp.lang.ruby)
  • Re: 386sx/25mhz compatibility
    ... I'd need to add hard drives also, ... a better computer out of the garbage. ... Pentium, mid-2001, suddenly better computers started appearing. ... later, I got a 50MHz 486 with 16megs of memory, for ten dollars. ...
    (comp.os.linux.hardware)
  • Re: FileStream.Close() & GarbageCollection - Memory Leak Question
    ... I considered the memory a "resource" that would be freed but I have no ... The only resource that's truly "managed" is memory in the garbage ... managed objects can still "own" unmanaged resources (pointers ... You could think of the handle like a dry cleaning ticket. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A debugging implementation of malloc
    ... when allocating memory, fill it with one kind of garbage ... memory), and when releasing it, fill it with another kind of garbage ... access freed memory). ...
    (comp.lang.c)
  • Re: Larry Wall, on Tcl
    ... Sure, if each chunk of memory allocated was only freed in one place, it wouldn't be too painful. ... It keeps the bug from being noticed unless the subsystem using slow physical memory to back the virtual memory is overwhelmed. ... Both of those are prevented by garbage collection. ... What do you do to explicitly free up registers in your C code when you're done with them? ...
    (comp.lang.tcl)