Hi,
Shouldn't ReallocMem in CMem zero the newly allocated bytes ?
Micha

Mailing lists / fpc-devel / 2006-03
12 messages · 5 people ·
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hi,
Shouldn't ReallocMem in CMem zero the newly allocated bytes ?
Micha
Op Wed, 8 Mar 2006, schreef Micha Nelissen:
Hi, Shouldn't ReallocMem in CMem zero the newly allocated bytes ?
No, it is the responsibility of the programmer.
Daniël
Nope. At least the compiler depends on it being zeroed. The FPC heap manager zeroes it, as well.
Micha
Where? It's a bug if it does.
The FPC heap manager zeroes it, as well.
It shouldn't need to do that.
Jonas
On Wed, 8 Mar 2006 18:10:38 +0100 (CET) Nope. At least the compiler depends on it being…
Op Wed, 8 Mar 2006, schreef Micha Nelissen:
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.
Daniël
On Wed, 8 Mar 2006 18:10:38 +0100 (CET) Nope. At least the compiler depends on it being…
Good.
Nope. At least the compiler depends on it being zeroed. The FPC heap manager zeroes it, as well.
Not under linux. Are you sure?
Mattias
> Nope. At least the compiler depends on it being zeroed. The FPC heap > manager zeroes it, as well. Not under linux. Are you sure?
Hmm, that was changed then and the compiler was fixed; probably due to me mentioning it on IRC sometime ago, also :-). The name has 'alloc' in it, so maybe that's why I keep getting confused, ResizeMem or so would be more appropriate.
Micha
What has 'alloc' to do with cleaning/zeroing?
Mattias
It is *the* difference between GetMem and AllocMem.
Micha
Not in FPC. Reallocmem just resizes. No zeroing is done, as already explained by Jonas and Daniel.
Only classes/objects that are allocated on the heap are zeroed. Plain memory is not touched in any way.
Michael.
Yes, but allocmem() does zero the allocated memory (although our documentation at http://www.freepascal.org/docs-html/rtl/system/ allocmem.html claims it's the same as getmem, that's not true -- I guess because in Delphi it also zeroes the allocated memory with allocmem).
Jonas
I'll change the docs accordingly.
Michael.