Download Programming with lcc

Transcript
120
C programming with lcc-win32
the same as another random piece of memory, a catastrophe in most cases. You write to a variable and without you knowing it, you are writing to another variable at the same time, destroying all data stored there.
More easy to find, since more or less it always provokes a trap, the dangling pointer problem
can at any moment become the dreaded show stopper bug that crashes the whole program and
makes the user of your program loose all the data he/she was working with.
I would be delighted to tell you how to avoid those bugs, but after more than 10 years working
with the C language, I must confess to you that memory management bugs still plague my programs, as they plague all other C programmers.68
The basic problem is that the human mind doesn’t work like a machine, and here we are asking people (i.e. programmers) to be like machines and keep book exactly of all the many small
pieces of RAM a program uses during its lifetime without ever making a mistake.
But there is a solution that I have implemented in lcc-win32. Lcc-win32 comes with an automatic memory manager (also called garbage collector in the literature) written by Hans
Boehm. This automatic memory manager will do what you should do but do not want to do:
take care of all the pieces of RAM for you.
Using the automatic memory manager you just allocate memory with GC_malloc instead of
allocating it with malloc. The signature (i.e. the result type and type of arguments) is the same
as malloc, so by just replacing all malloc by GC_malloc in your program you can benefit of
the automatic memory manager without writing any new line of code.
The memory manager works by inspecting regularly your whole heap and stack address space,
and checking if there is anywhere a reference to the memory it manages. If it doesn’t find any
references to a piece of memory it will mark that memory as free and recycle it. It is a very
simple schema, taken to almost perfection by several years of work from the part of the
authors.
To use the memory manager you should add the gc.lib library to your link statement or indicate that library in the IDE in the linker configuration tab.
1.27.1
Functions for memory allocation
malloc
Returns a pointer to a newly allocated memory block
free
Releases a memory block
calloc
Returns a pointer to a newly allocated zero-filled memory block.
realloc
Resizes a memory block preserving its contents.
alloca
Allocate a memory block in the stack that is automatically destroyed when the function
where the allocation is requested exits.
_msize
Returns the size of a block
_expand
Increases the size of a block without moving it.
GC_malloc
Allocates a memory block managed by the memory manager.
1.27.2
Memory layout under windows69
A 32 bit address can be used to address up to 4GB of RAM. From this potential address space,
windows reserves for the system 2GB, leaving the other 2GB for each application. These
68. This discussion is based upon the article of Randy Kath, published in MSDN.