[fpc-devel] ReallocMem with CMem

Daniël Mantione daniel.mantione at freepascal.org
Wed Mar 8 18:31:02 CET 2006

Op Wed, 8 Mar 2006, schreef Micha Nelissen:

> On Wed, 8 Mar 2006 18:10:38 +0100 (CET)
> Daniël Mantione <daniel.mantione at freepascal.org> wrote:
> > 
> > 
> > Op Wed, 8 Mar 2006, schreef Micha Nelissen:
> > 
> > > Shouldn't ReallocMem in CMem zero the newly allocated bytes ?
> > 
> > No, it is the responsibility of the programmer.
> Nope. At least the compiler depends on it being zeroed. The FPC heap
> manager zeroes it, as well.

That is behaviour contrary to the documentation.

Our documentation:

   ReAllocMem resizes the memory pointed to by P so it has size Size. The  
   value of P may change during this operation. The contents of the memory 
   pointed to by P (if any) will be copied to the 
   new location, but may be truncated if the newly
   allocated memory block is smaller in size. If a larger block is 
   allocated, only the used memory is initialized, extra
   memory will not be zeroed out.

   Note that P may be nil, in that case the behaviour of ReAllocMem is 
   equivalent to Getmem.

Delphi documentation:

    The ReallocMem procedure changes the storage size of    
    an existing block of storage.            
    The size may be smaller or bigger.       
    If smaller, then the existing storage is effectively 
    truncated, the remainder being released.             
    If larger, the storage is extended. Or a new block     
    is allocated if necessary. In either case, the         
    It is better to use New for this
    existing storage block data is preserved, and the  
    new part is unitialised.                                   

There is a good reason that the memory should not be initialised; you can 
allocate large amounts of memory in advance. This memory is not actually 
allocated by the OS until you use it. As soon as you initialize the 
memory, it gets allocated immediately.

If the FPC heap manager initialises the memory, that functionality should 
be removed.


More information about the fpc-devel mailing list