[SDL] ot - memory leaks

Lilith Calbridge lilith at dcccd.edu
Sat Aug 4 12:50:17 PDT 2007



>>> On 8/4/2007 at 2:17 PM, <neil at cloudsprinter.com> wrote:

>> I use a simple memory manager for C code to catch memory leaks. Not

>> fancy, but
>> it fits my needs. I've packaged it for anyone who might be
interested. You
>> can download the 16KB zip file from
>>
>> http://home.pacbell.net/theposts/memmgr.zip 
>>
> thanks for this, i'll take a look, but i guess what i really need to
do is 
> go
> and find a big fat book on C at the library, because i still dont
really 
> quite
> get using the malloc stuff, so don't because it scares me ;)

I used to be afraid of malloc but when I got into a program that
required it and became comfortable with it.
 
> i have been sttempting to use free() but it all seems very obscure, 
> someimes it
> works, sometimes it dosnt. but i'm sure that's all my fault ;)

The one thing to remember about using malloc/free is to keep track of
your pointers' states.  Initialize all pointers to NULL when they're
declared.  Always check before assigning a pointer to malloc'd space. 
If it isn't NULL, you don't want to assign it.  Always check the
returned value when you malloc to make sure that space was assigned. 
When you free the memory a pointer is pointing to, always reassign the
pointer to NULL so that you know that the next time you need to use the
pointer you can make sure that it isn't already pointing to assigned
memory.  And always keep track of how much space you requested and never
store outside its bounds.
 
-- 
Lilith


More information about the SDL mailing list