[fpc-pascal] Array clearing
Sven Barth
pascaldragon at googlemail.com
Tue Apr 4 17:10:56 CEST 2017
On 04.04.2017 16:27, Ryan Joseph wrote:
>>> Does SetLength on a single level dynamic array not even allocate a continuous block of memory?
>>
>> Yes, it does (as explained in all the other mails).
>> A (dynamic) array of integer will be allocated as a single block by SetLength.
>> So if you only have one level of a dynamic array as in
>>
>> var MyArray : array of integer;
>>
>> then SetLength(MyArray,1000) will allocate a single block of 1000 integers.
>> But with
>>
>> var MyArray : array of array of integer;
>>
>> SetLength(MyArray,1000);
>>
>> will allocate a single block of 1000 pointers (to an array of integer each).
>> Then, SetLength(MyArray[0],1000) will allocate one (!) block of 1000 integers.
>> SetLength(MyArray[1],1000) will allocate another block of 1000 integers and so on….
>
> I was allocating using SetLength(MyArray, 3, 3, 3) for a 3x3x3 matrix for example. Maybe it depends on the memory manager and the state of heap but if you called early in the programs execution it should be allocate all that memory in one block I would think.
While your statement regarding allocation might be true you must not
forget that a dynamic array consists of a meta data block (length,
reference count) that is located directly in front of the data block. So
even if the memory blocks would be allocated consecutively then there'd
still be the meta data blocks inbetween.
If you already know that your dynamic arrays only have a specific size
(for matrices used in games that should usually be the case) then you're
better off with using a static array:
=== code begin ===
type
TMatrix = array[0..2, 0..2, 0..2] of LongInt;
=== code end ===
There you can use FillChar() as much as you want as that is indeed a
single memory block containing 9 LongInt elements.
Regards,
Sven
More information about the fpc-pascal
mailing list