Re: [fpc-pascal] Array clearing

2017-04-13 Thread Sven Barth via fpc-pascal
Am 13.04.2017 18:06 schrieb "Ryan Joseph" : > > > > On Apr 13, 2017, at 10:29 PM, Sven Barth via fpc-pascal < fpc-pascal@lists.freepascal.org> wrote: > > > > SetLength *does* use a reallocate for this, but since it doesn't give you any guarantee for its success as its

Re: [fpc-pascal] Array clearing

2017-04-13 Thread Ryan Joseph
> On Apr 13, 2017, at 10:29 PM, Sven Barth via fpc-pascal > wrote: > > SetLength *does* use a reallocate for this, but since it doesn't give you any > guarantee for its success as its the task of the memory manager to deal with > this, it's easier to assume

Re: [fpc-pascal] Array clearing

2017-04-13 Thread Sven Barth via fpc-pascal
Am 13.04.2017 14:47 schrieb "Ryan Joseph" : > > > > On Apr 13, 2017, at 7:08 PM, Mattias Gaertner wrote: > > > >> as I understood from (http://wiki.freepascal.org/Dynamic_array< http://wiki.freepascal.org/Dynamic_array>), SetLength will

Re: [fpc-pascal] Array clearing

2017-04-13 Thread Mattias Gaertner
On Thu, 13 Apr 2017 19:15:13 +0700 Ryan Joseph wrote: > > On Apr 13, 2017, at 7:08 PM, Mattias Gaertner > > wrote: > > > >> as I understood from > >> (http://wiki.freepascal.org/Dynamic_array),

Re: [fpc-pascal] Array clearing

2017-04-13 Thread Ryan Joseph
> On Apr 13, 2017, at 7:08 PM, Mattias Gaertner > wrote: > >> as I understood from >> (http://wiki.freepascal.org/Dynamic_array), >> SetLength will create a copy of the array and free the memory of the >> shorter array. In

Re: [fpc-pascal] Array clearing

2017-04-13 Thread Sven Barth via fpc-pascal
Am 13.04.2017 13:25 schrieb "MARCOU Gilles" : > > Regarding this code: > >> SetLength(Array,Length(Array)+1); >> Array[High(Array)] := … > > > as I understood from (http://wiki.freepascal.org/Dynamic_array), SetLength will create a copy of the array and free the memory of the

Re: [fpc-pascal] Array clearing

2017-04-13 Thread Mattias Gaertner
On Thu, 13 Apr 2017 13:17:37 +0200 MARCOU Gilles wrote: > Regarding this code: > > > SetLength(Array,Length(Array)+1); > > Array[High(Array)] := … > > as I understood from (http://wiki.freepascal.org/Dynamic_array > ), SetLength

Re: [fpc-pascal] Array clearing

2017-04-13 Thread MARCOU Gilles
Regarding this code: > SetLength(Array,Length(Array)+1); > Array[High(Array)] := … as I understood from (http://wiki.freepascal.org/Dynamic_array ), SetLength will create a copy of the array and free the memory of the shorter array. In this case, a lot

Re: [fpc-pascal] Array clearing

2017-04-13 Thread Benito van der Zander
Hi Ryan, In the real world is anyone actually going to type that out every time? I’d bet most of us have an array class we use and ignore dynamic arrays all together because we all need basic operations like “add” and “remove”. I, for one, have a huge list of array utility functions:

Re: [fpc-pascal] Array clearing

2017-04-13 Thread Jürgen Hestermann
Am 2017-04-12 um 16:30 schrieb Ryan Joseph: >> Array[High(Array)] := whateveryouwant; > SetLength(Array,Length(Array)+1); > Array[High(Array)] := … > In the real world is anyone actually going to type that out every time? Yes, I do this. Typing is not much work for me. My main focus is on

Re: [fpc-pascal] Array clearing

2017-04-13 Thread Ryan Joseph
> On Apr 12, 2017, at 9:24 PM, Jürgen Hestermann > wrote: > > SetLength(Array,Length(Array)+1); > > You will get an additional element (filled with zeros). > Then you can set > > Array[High(Array)] := whateveryouwant; Well sure, but this kind of code is exactly

Re: [fpc-pascal] Array clearing

2017-04-12 Thread Michael Van Canneyt
On Wed, 12 Apr 2017, Ryan Joseph wrote: On Apr 12, 2017, at 9:25 PM, Michael Van Canneyt wrote: Adding a pop/push requires compiler magic, and could be implemented; but the question is whether it is worth it, given the plethora of other options at your disposal.

Re: [fpc-pascal] Array clearing

2017-04-12 Thread Sven Barth via fpc-pascal
Am 12.04.2017 16:51 schrieb "Ryan Joseph" : > > > > On Apr 12, 2017, at 9:25 PM, Michael Van Canneyt wrote: > > > > Adding a pop/push requires compiler magic, and could be implemented; but the question is whether it is worth it, given the

Re: [fpc-pascal] Array clearing

2017-04-12 Thread Jürgen Hestermann
Am 2017-04-12 um 16:36 schrieb Ryan Joseph: > Why magic? It seems silly the operator += doesn’t exist Well, are we using Pascal or C? ;-) > or even a basic operation set like “add”, “remove”, “insert” that every array implementation in every language has. > Aren’t those just function around

Re: [fpc-pascal] Array clearing

2017-04-12 Thread Ryan Joseph
> On Apr 12, 2017, at 9:25 PM, Michael Van Canneyt > wrote: > > Adding a pop/push requires compiler magic, and could be implemented; but the > question is whether it is worth it, given the plethora of other > options at your disposal. Why magic? It seems silly the

Re: [fpc-pascal] Array clearing

2017-04-12 Thread Ryan Joseph
> On Apr 12, 2017, at 9:43 PM, Jürgen Hestermann > wrote: > > "clear" is not very clear to me ;-) > IMO SetLength(array,0) is clearing too. SetLength resizes memory so there’s that distinction. The fact there is an API is what’s important because there’s at least a

Re: [fpc-pascal] Array clearing

2017-04-12 Thread Michael Van Canneyt
On Wed, 12 Apr 2017, Jürgen Hestermann wrote: Am 2017-04-12 um 16:17 schrieb Ryan Joseph: > SetLength resizes the array in memory and “clear” means setting all values to default() for that type. "clear" is not very clear to me ;-) IMO SetLength(array,0) is clearing too. The function you

Re: [fpc-pascal] Array clearing

2017-04-12 Thread Jürgen Hestermann
Am 2017-04-12 um 16:17 schrieb Ryan Joseph: > SetLength resizes the array in memory and “clear” means setting all values to default() for that type. "clear" is not very clear to me ;-) IMO SetLength(array,0) is clearing too. The function you want should be called "Fill". For example a function

Re: [fpc-pascal] Array clearing

2017-04-12 Thread Michael Van Canneyt
On Wed, 12 Apr 2017, Ryan Joseph wrote: On Apr 12, 2017, at 8:05 PM, nore...@z505.com wrote: Since fillchar is not a general way to clear an item it almost seems like pascal needs a way to clear things generically, such as Array.clear But I guess this would be reinventing OOP, or Ruby

Re: [fpc-pascal] Array clearing

2017-04-12 Thread Jürgen Hestermann
Am 2017-04-12 um 15:46 schrieb Ryan Joseph: > I agree. Dynamic arrays only advantage is they are managed by the compiler but they’re missing obvious functionality like adding/removing elements so they’re usually pretty useless. > Why was the functionality of appending an element to the end

Re: [fpc-pascal] Array clearing

2017-04-12 Thread Ryan Joseph
> On Apr 12, 2017, at 8:05 PM, nore...@z505.com wrote: > > Since fillchar is not a general way to clear an item it almost seems like > pascal needs a way to clear things generically, such as Array.clear > But I guess this would be reinventing OOP, or Ruby where everything is an > object and

Re: [fpc-pascal] Array clearing

2017-04-12 Thread Ryan Joseph
> On Apr 12, 2017, at 9:07 PM, Jürgen Hestermann > wrote: > > What exactly does it do when "clearing" a dynamic array? > There are multiple ways to "clear" it. I see your point but there could be a good API to make this clear. SetLength resizes the array in memory

Re: [fpc-pascal] Array clearing

2017-04-12 Thread Jürgen Hestermann
Am 2017-04-12 um 15:05 schrieb nore...@z505.com: > Since fillchar is not a general way to clear an item Define "clear". There are many things that can be named "clearing" (see below). > I've never been a fan of things like fillchar as it seems like a reinvention of something more like Clear()

Re: [fpc-pascal] Array clearing

2017-04-12 Thread noreply
On 2017-04-01 02:39, Sven Barth via fpc-pascal wrote: Am 01.04.2017 05:42 schrieb "Ryan Joseph" : As far as the compiler is concerned what’s the difference between clearing an array using a for-loop vs. FillChar? It seems like iterating the array would be slower

Re: [fpc-pascal] Array clearing

2017-04-05 Thread Jonas Maebe
On 05/04/17 04:49, Ryan Joseph wrote: Yeah after all this talk, I’m going to use array[0..0, 0..0, 0..0] and allocate the memory myself to avoid overhead and confusion. Thanks for explaining everything to me but this time going low level makes the most sense. Then you lose the ability to

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Ryan Joseph
> On Apr 4, 2017, at 11:50 PM, Jürgen Hestermann > wrote: > > I am trying to show the memory allocation for the 10x10 array as a "graphic": > > arr --> arr[0],arr[1],arr[2],arr[3],arr[4],arr[5],arr[6],arr[7],arr[8],arr[9] > | | | | |

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Ryan Joseph
> On Apr 4, 2017, at 10:10 PM, Sven Barth via fpc-pascal > wrote: > > 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

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Jürgen Hestermann
Am 2017-04-04 um 16:54 schrieb Ryan Joseph: >> var arr : array of array of Integer; >> begin >> SetLength(arr, 10, 10); > “then the first array stores a pointer to each sub array.” > Could you illustrate this is code? > I don’t think I’m understanding this exactly like it’s represented in memory.

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Sven Barth via fpc-pascal
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

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Sven Barth via fpc-pascal
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

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Mattias Gaertner
On Tue, 4 Apr 2017 16:46:02 +0200 Sven Barth via fpc-pascal wrote: >[...] > SetLength() allocates a single block of memory, To avoid misunderstanding: SetLength(a,dim1,dim2) allocates one block for the dim1 array and then for each element another block. Mattias

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Ryan Joseph
> On Apr 4, 2017, at 10:07 PM, Jürgen Hestermann > wrote: > > Am 2017-04-04 um 15:40 schrieb Ryan Joseph: > > I’m glad I asked because of arrays of pointers is bad news for performance. > > I don't think that you will notice a performance issue with dynamic arrays

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Sven Barth via fpc-pascal
On 04.04.2017 16:54, Ryan Joseph wrote: > >> On Apr 4, 2017, at 9:46 PM, Sven Barth via fpc-pascal >> wrote: >> >> SetLength() allocates a single block of memory, cause array access is >> ordinary pointer arithmetic. However if you have an array of array then >>

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Sven Barth via fpc-pascal
On 04.04.2017 15:40, Ryan Joseph wrote: > >> On Apr 4, 2017, at 7:17 PM, Sven Barth via fpc-pascal >> wrote: >> >> If you want continuous memory areas you need to use static arrays or develop >> your own dynamic data structure that uses array properties. >> >>

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Ryan Joseph
> On Apr 4, 2017, at 9:46 PM, Sven Barth via fpc-pascal > wrote: > > SetLength() allocates a single block of memory, cause array access is > ordinary pointer arithmetic. However if you have an array of array then > the first array stores a pointer to each sub

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Sven Barth via fpc-pascal
On 04.04.2017 15:40, Ryan Joseph wrote: > >> On Apr 4, 2017, at 7:17 PM, Sven Barth via fpc-pascal >> wrote: >> >> If you want continuous memory areas you need to use static arrays or develop >> your own dynamic data structure that uses array properties. >> >>

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Jürgen Hestermann
Am 2017-04-04 um 15:40 schrieb Ryan Joseph: > I’m glad I asked because of arrays of pointers is bad news for performance. I don't think that you will notice a performance issue with dynamic arrays (though it highly depends on the sizes and levels you use...) > Does SetLength on a single level

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Ryan Joseph
> On Apr 4, 2017, at 7:17 PM, Sven Barth via fpc-pascal > wrote: > > If you want continuous memory areas you need to use static arrays or develop > your own dynamic data structure that uses array properties. > > I’m glad I asked because of arrays of

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Jürgen Hestermann
Am 2017-04-04 um 15:18 schrieb Jürgen Hestermann: > var MyArray : array of array of integer; > > you can do: > > SetLength(MyArray,3); > SetLength(MyArray[0],2); > SetLength(MyArray[1],3); > SetLength(MyArray[2],4); > > So MyArray[0] points to an array of 2 integers, > MyArray[1] points to an

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Jürgen Hestermann
Am 2017-04-04 um 13:55 schrieb Ryan Joseph: > Doesn’t iterating the array default the purpose of FillChar? > The goal was the most efficient way clear the array with zero’s. > Even if the array if nested 3 levels deep (anArray[x][y][z]) > it should (I hope) be a contiguous block of memory that

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Sven Barth via fpc-pascal
Am 04.04.2017 13:55 schrieb "Ryan Joseph" : > > > > On Apr 4, 2017, at 4:58 PM, Howard Page-Clark via fpc-pascal < fpc-pascal@lists.freepascal.org> wrote: > > > > You can always use FillChar and its kin on specific 'nested' arrays like this > > > > type > > TIntArray =

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Ryan Joseph
> On Apr 4, 2017, at 4:58 PM, Howard Page-Clark via fpc-pascal > wrote: > > You can always use FillChar and its kin on specific 'nested' arrays like this > > type > TIntArray = array of Integer; > TIntIntArray = array of TIntArray; > TIntIntIntArray = array

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Sven Barth via fpc-pascal
Am 04.04.2017 12:52 schrieb "Mark Morgan Lloyd" < markmll.fpc-pas...@telemetry.co.uk>: > > On 02/04/17 10:00, Jonas Maebe wrote: > >> Allocating new memory via setlength also clears the memory (+ the >> overhead of allocating the memory). > > > Jonas, is it still the case that if SetLength()

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Jürgen Hestermann
Am 2017-04-04 um 12:51 schrieb Mark Morgan Lloyd: > On 02/04/17 10:00, Jonas Maebe wrote: >> Allocating new memory via setlength also clears the memory (+ the >> overhead of allocating the memory). > Jonas, is it still the case that if SetLength() results in existing data being moved that the

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Mark Morgan Lloyd
On 02/04/17 10:00, Jonas Maebe wrote: Allocating new memory via setlength also clears the memory (+ the overhead of allocating the memory). Jonas, is it still the case that if SetLength() results in existing data being moved that the original- which might be e.g. an unencrypted password-

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Howard Page-Clark via fpc-pascal
On 04/04/17 05:25, Ryan Joseph wrote: Is it possible use FillChar on a multidimensional arrays? arr: array of array of array of integer. SetLength(arr, 3, 3, 3); FillChar(arr[0], (3*3*3)*sizeof(integer), false); I’m just getting crashes. You can always use FillChar and its kin on specific

Re: [fpc-pascal] Array clearing

2017-04-04 Thread Sven Barth via fpc-pascal
Am 04.04.2017 06:55 schrieb "Ryan Joseph" : > > > > On Apr 2, 2017, at 11:02 PM, Sven Barth via fpc-pascal < fpc-pascal@lists.freepascal.org> wrote: > > > > can be easily seen by looking at the implementation of SetLength() in > > $fpcdir/rtl/inc/dynarr.inc,

Re: [fpc-pascal] Array clearing

2017-04-03 Thread Ryan Joseph
> On Apr 2, 2017, at 11:02 PM, Sven Barth via fpc-pascal > wrote: > > can be easily seen by looking at the implementation of SetLength() in > $fpcdir/rtl/inc/dynarr.inc, fpc_dynarray_setlength(). > Especially since line 220 (at least in the current revision)

Re: [fpc-pascal] Array clearing

2017-04-02 Thread Sven Barth via fpc-pascal
On 02.04.2017 11:22, Ryan Joseph wrote: > >> On Apr 1, 2017, at 9:25 PM, Jürgen Hestermann >> wrote: >> >> If you just need to reuse the same array and only need to zero its elements >> then of course fillchar would be the fastest approach (it saves the memory >>

Re: [fpc-pascal] Array clearing

2017-04-02 Thread Jonas Maebe
On 02/04/17 11:22, Ryan Joseph wrote: On Apr 1, 2017, at 9:25 PM, Jürgen Hestermann wrote: If you just need to reuse the same array and only need to zero its elements then of course fillchar would be the fastest approach (it saves the memory reallocation step). Why

Re: [fpc-pascal] Array clearing

2017-04-02 Thread Ryan Joseph
> On Apr 1, 2017, at 9:25 PM, Jürgen Hestermann > wrote: > > If you just need to reuse the same array and only need to zero its elements > then of course fillchar would be the fastest approach (it saves the memory > reallocation step). Why is this “of course”?

Re: [fpc-pascal] Array clearing

2017-04-01 Thread wkitty42
On 04/01/2017 02:33 PM, Jürgen Hestermann wrote: Am 2017-04-01 um 19:42 schrieb wkitt...@windstream.net: consider this: when using a string var and writing to a binary file... you reuse the string var for each value written... if you don't clear the string var between fills then the binary will

Re: [fpc-pascal] Array clearing

2017-04-01 Thread Jürgen Hestermann
Am 2017-04-01 um 19:42 schrieb wkitt...@windstream.net: > consider this: when using a string var and writing to a binary file... > you reuse the string var for each value written... > if you don't clear the string var between fills then the binary will > contain "garbage" in the unused positions

Re: [fpc-pascal] Array clearing

2017-04-01 Thread wkitty42
On 04/01/2017 07:31 AM, Jürgen Hestermann wrote: I am wondering what the purpose of filling all array elements with zero's could be. If I want to discard all elements I would simply delete the whole array (setlength(MyArray,0) ). consider this: when using a string var and writing to a binary

Re: [fpc-pascal] Array clearing

2017-04-01 Thread Jürgen Hestermann
Am 2017-04-01 um 15:09 schrieb Ryan Joseph: > Because the array is being iterated and I need to know which values are set. > Basically I have a dynamic array I grow to a certain size and this process happens in a loop. > The options are to allocate/free the array every cycle or clear memory

Re: [fpc-pascal] Array clearing

2017-04-01 Thread Ryan Joseph
> On Apr 1, 2017, at 6:31 PM, Jürgen Hestermann > wrote: > > I am wondering what the purpose of filling all > array elements with zero's could be. > If I want to discard all elements I would simply delete > the whole array (setlength(MyArray,0) ). Because the array

Re: [fpc-pascal] Array clearing

2017-04-01 Thread Sven Barth via fpc-pascal
Am 01.04.2017 13:31 schrieb "Jürgen Hestermann" : > > I am wondering what the purpose of filling all > array elements with zero's could be. > If I want to discard all elements I would simply delete > the whole array (setlength(MyArray,0) ). > > But when should it be

Re: [fpc-pascal] Array clearing

2017-04-01 Thread Jürgen Hestermann
I am wondering what the purpose of filling all array elements with zero's could be. If I want to discard all elements I would simply delete the whole array (setlength(MyArray,0) ). But when should it be useful to keep all elements and just overwrite them all with zero's (which is also very time

Re: [fpc-pascal] Array clearing

2017-04-01 Thread Sven Barth via fpc-pascal
Am 01.04.2017 10:35 schrieb "Ryan Joseph" : > > > > On Apr 1, 2017, at 2:50 PM, Ryan Joseph wrote: > > > > Yeah, I was concerned with just compiler types or weakly retained classes where I’m just keeping the reference. > > Another question.

Re: [fpc-pascal] Array clearing

2017-04-01 Thread Ryan Joseph
> On Apr 1, 2017, at 2:50 PM, Ryan Joseph wrote: > > Yeah, I was concerned with just compiler types or weakly retained classes > where I’m just keeping the reference. Another question. Is it more efficient/faster to reallocate a new array of the same size or call

Re: [fpc-pascal] Array clearing

2017-04-01 Thread Ryan Joseph
> On Apr 1, 2017, at 2:39 PM, Sven Barth via fpc-pascal > wrote: > > It totally depends on the type. In case of primitive types like integers > there is indeed only the performance difference (though if you know that the > element size is four FillDWord could

Re: [fpc-pascal] Array clearing

2017-04-01 Thread Sven Barth via fpc-pascal
Am 01.04.2017 05:42 schrieb "Ryan Joseph" : > > As far as the compiler is concerned what’s the difference between clearing an array using a for-loop vs. FillChar? It seems like iterating the array would be slower but what does FillChar do exactly and is it faster? The