[Re: [AD] mode-list updates.]

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


>   Like `malloc', `realloc' may return a null pointer if no memory
>space is available to make the block bigger.  When this happens, the
>original block is untouched; it has not been modified or relocated.

I haven't looked at the Allegro code so I hope I'm not being irritatingly obvious but, this is the reason why you have to be careful with code like:

mem = realloc(mem, newsize);

because if realloc returns NULL, then the previous memory is still allocated and needs freeing, but variable mem doesn't point there
anymore.  So you end up having to be like:

/* Try to reallocate */
void * newmem = realloc(mem, newsize);
if (newmem)
   mem = newmem;

To make sure you don't have a leak and that you can free it eventually.  It does make realloc code less clean that it 
appears it could be.

Sorry if this is all too obvious...

Doug Eleveld


------------------------------------------------------------
--== Sent via Deja.com ==--
http://www.deja.com/



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/