Sign in Join

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

Mailing lists / fpc-devel / 2013-12

Manual memory management with ansistrings and dynamic arrays

5 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Adriaan van Os

Is it safe to assume that manually allocating a cleared memory block (e.g. with calloc rather than New) sufficiently initializes the ansistrings and dynamic arrays in that memory block ? If not, what does the init code look like that New is calling/setting up for ansistrings and dynamic arrays ?

Regards,

Adriaan van Os

Reply by email

Jonas Maebe
to Adriaan van Os
Adriaan van Os wrote 5 lines
Is it safe to assume that manually allocating a cleared memory block (e.g. with calloc rather than New) sufficiently initializes the ansistrings and dynamic arrays in that memory block ? If not, what does the init code look like that New is calling/setting up for ansistrings and dynamic arrays ?

In practice, that is currently enough.

In theory, you should call "initialize" for every record/array containing management types in your allocated block to be safe.

Jonas

Reply by email

Jonas Maebe
to Jonas Maebe
Jonas Maebe wrote 12 lines
On 12 Dec 2013, at 16:05, Adriaan van Os wrote: > Is it safe to assume that manually allocating a cleared memory > block (e.g. with calloc rather than New) sufficiently initializes > the ansistrings and dynamic arrays in that memory block ? If not, > what does the init code look like that New is calling/setting up > for ansistrings and dynamic arrays ? In practice, that is currently enough. In theory, you should call "initialize" for every record/array containing management types in your allocated block to be safe.

And in practice, you also have to call "finalize" again for every record/array containing managed types (and for every separate managed type too, I forgot to mention this earlier although I hope that was implicitly clear) in that allocated block before freeing it again (to prevent memory leaks).

Jonas

Reply by email

Adriaan van Os
to Jonas Maebe
Jonas Maebe wrote 8 lines
> In theory, you should call "initialize" for every record/array > containing management types in your allocated block to be safe. And in practice, you also have to call "finalize" again for every record/array containing managed types (and for every separate managed type too, I forgot to mention this earlier although I hope that was implicitly clear) in that allocated block before freeing it again (to prevent memory leaks).

Thanks for your reply. I feel comfortable calling "initialize" and "finalize" for every managed type in the memory block that I allocate/deallocate manually. However, can you elaborate why "initialize" and "finalize" must be called for the containing record/array too ? If my understanding of your reply is correct ?

Regards,

Adriaan van Os

Reply by email

Jonas Maebe
to Adriaan van Os
Adriaan van Os wrote 15 lines
Jonas Maebe wrote: >> In theory, you should call "initialize" for every record/array >> containing management types in your allocated block to be safe. > And in practice, you also have to call "finalize" again for every > record/array containing managed types (and for every separate > managed type too, I forgot to mention this earlier although I hope > that was implicitly clear) in that allocated block before freeing > it again (to prevent memory leaks). Thanks for your reply. I feel comfortable calling "initialize" and "finalize" for every managed type in the memory block that I allocate/deallocate manually. However, can you elaborate why "initialize" and "finalize" must be called for the containing record/ array too ? If my understanding of your reply is correct ?

If you call it for an array/record containing managed types, then then compiler/rtl will initialise all managed fields/elements within that array/record. Calling it for individual elements is only required if they're not contained in a record/array.

Jonas

Reply by email