Installing SMARTALLOC
SMARTALLOC is provided as a Zipped archive, smartall.zip; see the download instructions below.
To install SMARTALLOC in your program, simply add the statement:
to every C program file which calls any of the memory allocation
functions (malloc
, calloc
, free
, etc.). SMARTALLOC must be
used for all memory allocation with a program, so include file for your
entire program, if you have such a thing. Next, define the symbol
SMARTALLOC in the compilation before the inclusion of smartall.h. I
usually do this by having my Makefile add the “-DSMARTALLOC
” option
to the C compiler for non-production builds. You can define the symbol
manually, if you prefer, by adding the statement:
#define SMARTALLOC
At the point where your program is all done and ready to relinquish control to the operating system, add the call:
sm_dump(datadump);
where datadump specifies whether the contents of orphaned buffers are
to be dumped in addition printing to their size and place of allocation.
The data are dumped only if datadump is nonzero, so most programs will
normally use “sm_dump(0);
”. If a mysterious orphaned buffer appears
that can’t be identified from the information this prints about it,
replace the statement with “sm_dump(1)
;”. Usually the dump of the
buffer’s data will furnish the additional clues you need to excavate and
extirpate the elusive error that left the buffer allocated.
Finally, add the files “smartall.h” and “smartall.c” from this release
to your source directory, make dependencies, and linker input. You
needn’t make inclusion of smartall.c in your link optional; if compiled
with SMARTALLOC not defined it generates no code, so you may always
include it knowing it will waste no storage in production builds. Now
when you run your program, if it leaves any buffers around when it’s
done, each will be reported by sm_dump()
on stderr as follows:
Orphaned buffer: 120 bytes allocated at line 50 of gutshot.c
See also
Possible Next Steps
Go back to Smart Memory Allocation.
Go back to Developer Guide.