Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2006-03

ReallocMem with CMem

12 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Micha Nelissen
to Daniël Mantione
Show quoted text 7 lines
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.

Micha

Reply by email

Jonas Maebe
to Micha Nelissen
Micha Nelissen wrote 5 lines
>> 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.

Where? It's a bug if it does.

The FPC heap manager zeroes it, as well.

It shouldn't need to do that.

Jonas

Reply by email

Daniël Mantione
to Micha Nelissen: 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:

Show quoted text 13 lines
On Wed, 8 Mar 2006 18:10:38 +0100 (CET) Daniël Mantione 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.

Daniël

Reply by email

Mattias Gaertner
to Micha Nelissen: On Wed, 8 Mar 2006 18:10:38 +0100 (CET) Nope. At least the compiler depends on it being…
Show quoted text 10 lines
On Wed, 8 Mar 2006 18:10:38 +0100 (CET) Daniël Mantione 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.

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

Reply by email

Micha Nelissen
to Mattias Gaertner
> 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

Reply by email

Mattias Gaertner
to Micha Nelissen
Show quoted text 12 lines
On Wed, 8 Mar 2006 18:33:53 +0100 Mattias Gaertner wrote: > > 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.

What has 'alloc' to do with cleaning/zeroing?

Mattias

Reply by email

Micha Nelissen
to Mattias Gaertner
Show quoted text 9 lines
On Wed, 8 Mar 2006 19:01:11 +0100 Micha Nelissen wrote: > 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. What has 'alloc' to do with cleaning/zeroing?

It is *the* difference between GetMem and AllocMem.

Micha

Reply by email

Michael Van Canneyt
to Micha Nelissen
Micha Nelissen wrote 14 lines
On Wed, 8 Mar 2006 19:10:55 +0100 Mattias Gaertner wrote: > On Wed, 8 Mar 2006 19:01:11 +0100 > Micha Nelissen wrote: > > > 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. > > What has 'alloc' to do with cleaning/zeroing? It is *the* difference between GetMem and AllocMem.

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.

Reply by email

Jonas Maebe
to Michael Van Canneyt
Michael Van Canneyt wrote 7 lines
>> What has 'alloc' to do with cleaning/zeroing? > > It is *the* difference between GetMem and AllocMem. Not in FPC. Reallocmem just resizes. No zeroing is done, as already explained by Jonas and Daniel.

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

Reply by email

Michael Van Canneyt
to Jonas Maebe
Jonas Maebe wrote 16 lines
On 8 mrt 2006, at 20:34, Michael Van Canneyt wrote: >>> What has 'alloc' to do with cleaning/zeroing? >> >> It is *the* difference between GetMem and AllocMem. > > Not in FPC. Reallocmem just resizes. No zeroing is done, as already > explained > by Jonas and Daniel. 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).

I'll change the docs accordingly.

Michael.

Reply by email