Dynamic Array Length

classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|

Dynamic Array Length

Rainer Stratmann
const
 data : array[ 1..{leave open} ] of byte = ( 11 , 12 , 13 );

 datalen = sizeof( data ) div data[ 1 ];

Would this possible in a way?
In C it is possible.

Now I have to know the length of the array data and say it before.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic Array Length

Anthony Walter-3
With Delphi/FPC you have a few choices:

Fixed length arrays, dynamic length arrays, and variant type arrays

Fixed length arrays cannot be resized and their lower and upper bounds are immutable. The lower and upper bounds can start at any ordinal position. The upper bound can ever be less than the upper bound. Also, you may use ordinal types as the bounds. Examples:

var
  ZeroBased: array[0..9] of Integer;
  OneBased: array[1..10] of Integer;
  Balance: array[-1..1] of Integer;
  Data: array[Byte] of Byte;
  BoolStr: array[Boolean] of string;

Dynamic length arrays are a managed type. That is to say the compiler generates to code to ensure the memory owned by the array is released when a procedure exits (if the array is a local). Kind of like the managed string type. You can use the SetLength() and Length() functions to resize/query them. You can also set them to nil, meaning an empty array. They cannot be used in a const block unless you set them to nil (which is kind of useless). Unlike the managed string type dynamic arrays do not copy on write. Dynamic arrays always use zero based indexing. Examples:

var
  Names: array of string;
  JaggedInts: array of array of Integer;

Both High and Low functions cna be used with fixed and dynamic arrays. Example:

for X := Low(JaggedInts) to High(JaggedInts) do
  for Y := Low(JaggedInts[X]) to High(JaggedInts[X]) do
    DoSomething(JaggedInts[X, Y]);

Variant type arrays are variants which reference TVarData having a VType of varArray. The following functions are used to manage variant arrays:

VarArrayCreate
VarArrayOf
VarArrayDimCount
VarArrayLowBound
VarArrayHighBound
ect


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic Array Length

Rainer Stratmann
Hello Anthony,

all I want is that the compiler calculates the len of the const array.

const
 data : array[ ... ] of byte = ( 11 , 12 , 13 );
 datalen = sizeof( data ) div data[ 0 ];

In C it is possible.

I think in FPC not :-(

Best regards, Rainer


Am Thursday 31 December 2009 14:54:37 schrieb Anthony Walter:

> With Delphi/FPC you have a few choices:
>
> Fixed length arrays, dynamic length arrays, and variant type arrays
>
> Fixed length arrays cannot be resized and their lower and upper bounds are
> immutable. The lower and upper bounds can start at any ordinal position.
> The upper bound can ever be less than the upper bound. Also, you may use
> ordinal types as the bounds. Examples:
>
> var
>   ZeroBased: array[0..9] of Integer;
>   OneBased: array[1..10] of Integer;
>   Balance: array[-1..1] of Integer;
>   Data: array[Byte] of Byte;
>   BoolStr: array[Boolean] of string;
>
> Dynamic length arrays are a managed type. That is to say the compiler
> generates to code to ensure the memory owned by the array is released when
> a procedure exits (if the array is a local). Kind of like the managed
> string type. You can use the SetLength() and Length() functions to
> resize/query them. You can also set them to nil, meaning an empty array.
> They cannot be used in a const block unless you set them to nil (which is
> kind of useless). Unlike the managed string type dynamic arrays do not copy
> on write. Dynamic arrays always use zero based indexing. Examples:
>
> var
>   Names: array of string;
>   JaggedInts: array of array of Integer;
>
> Both High and Low functions cna be used with fixed and dynamic arrays.
> Example:
>
> for X := Low(JaggedInts) to High(JaggedInts) do
>   for Y := Low(JaggedInts[X]) to High(JaggedInts[X]) do
>     DoSomething(JaggedInts[X, Y]);
>
> Variant type arrays are variants which reference TVarData having a VType of
> varArray. The following functions are used to manage variant arrays:
>
> VarArrayCreate
> VarArrayOf
> VarArrayDimCount
> VarArrayLowBound
> VarArrayHighBound
> ect


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic Array Length

Anthony Walter-3
No, the compiler will not fill in the array length based on your element list. It does compute the length while compiling and *you* need to match it in your declaration.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic Array Length

Rainer Stratmann
Am Thursday 31 December 2009 15:33:50 schrieb Anthony Walter:
> No, the compiler will not fill in the array length based on your element
> list. It does compute the length while compiling and *you* need to match it
> in your declaration.

But it would be a step to more userfriendly if the compiler calculates it
automatically. I would be possible.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic Array Length

Anthony Walter-3
Sure, I agree.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Const Array Length (was Dynamic Array Length)

Bugzilla from juha.manninen@phnet.fi
In reply to this post by Anthony Walter-3
On torstai, 31. joulukuuta 2009 16:33:50 Anthony Walter wrote:
> No, the compiler will not fill in the array length based on your element
> list. It does compute the length while compiling and *you* need to match it
> in your declaration.

In most cases it would be better if the compiler counted the number of
elements. Only in few cases the programmer really wants to limit the number to
some predefined value.
The currently used syntax for const array is:
const
  a: array[1..3] of string = ('aaa', 'bbb', 'ccc');

Already years ago I proposed the following syntax in Delphi mailing list:

const
  a: array of string = ('aaa', 'bbb', 'ccc');

It looks like a dynamic array, but obviously is not dynamic because it is
constant. :-)
The indexing would start from 0 like with dynamic arrays.
It would not break the existing syntax and would be very intuitive.
In the current syntax if I add one element, I need to change the upper bound
number, too, which feels really stupid. In the proposed syntax I would not
need to change any bounds.

In Delphi mailing list the answers were basically like:
The syntax is perfect now. Keep on changing the bounds.

How about FPC? This change would not be Delphi compatible apparently.


Regards,
Juha Manninen
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Const Array Length (was Dynamic Array Length)

José Mejuto
Hello FPC-Pascal,

Saturday, January 2, 2010, 4:01:35 PM, you wrote:

JM> In most cases it would be better if the compiler counted the number of
JM> elements. Only in few cases the programmer really wants to limit the number to
JM> some predefined value.
JM> The currently used syntax for const array is:
JM> const
JM>   a: array[1..3] of string = ('aaa', 'bbb', 'ccc');
JM> Already years ago I proposed the following syntax in Delphi mailing list:
JM> const
JM>   a: array of string = ('aaa', 'bbb', 'ccc');

From my point of view that's a dynamic array, if it looks like a
dynamic array ir should be a dynamic array.

Other pascals uses something like:

a: array[1..] or string = ('aaa', 'bbb', 'ccc');

or

a: array[..] or string = ('aaa', 'bbb', 'ccc');

I think that:

a: array[..n] or string = ('aaa', 'bbb', 'ccc');

is not allowed.

--
Best regards,
 JoshyFun

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Const Array Length (was Dynamic Array Length)

Jürgen Hestermann
In reply to this post by Bugzilla from juha.manninen@phnet.fi


> Juha Manninen schrieb:
> const
>   a: array of string = ('aaa', 'bbb', 'ccc');
>
> It looks like a dynamic array, but obviously is not dynamic because it is
> constant. :-)
> The indexing would start from 0 like with dynamic arrays.
> It would not break the existing syntax and would be very intuitive.

I would appreciate such a change. I often have static lists in my programs,
which need to be changed from time to time.
 
Ok, it's not a big deal to change the upper border (and if I forget it I am reminded ;-)),
but it would make life easier a bit if it could be automatic and it looks like an
intuitive enhancement in the spirit of pascal.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Const Array Length (was Dynamic Array Length)

Bugzilla from juha.manninen@phnet.fi
In reply to this post by José Mejuto
On lauantai, 2. tammikuuta 2010 17:34:57 JoshyFun wrote:
> From my point of view that's a dynamic array, if it looks like a
> dynamic array ir should be a dynamic array.

It can't be dynamic because it is under "const" section :-)

> Other pascals uses something like:
> a: array[1..] or string = ('aaa', 'bbb', 'ccc');
> or
> a: array[..] or string = ('aaa', 'bbb', 'ccc');

I have used only TP and Delphi and now some FPC. I didn't know of that syntax
but it looks perfect. The main point is that the programmer doesn't need to
calculate elements and set bounds.
IMO it should be implemented in FPC.


Regards,
Juha Manninen
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Const Array Length (was Dynamic Array Length)

Rainer Stratmann
In reply to this post by José Mejuto
Am Saturday 02 January 2010 16:34:57 schrieb JoshyFun:
>
> a: array[1..] or string = ('aaa', 'bbb', 'ccc');
>

That would be perfect.
Then you can automatically calculate the amount:
amax = sizeof( a ) div sizeof( a[ 1 ] );

And you are free to set the number of the first array, in this case 1.

Like Jürgen says it is not a big deal to change the upper border, but it would
make life easier a bit.

It would be a userfriendly bit.

May be people from delphi switch to fpc then... (ok not only for this reason,
but if there are another useful bits...)

Happy new year.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Const Array Length (was Dynamic Array Length)

Vincent Snijders-2
In reply to this post by Jürgen Hestermann
Jürgen Hestermann schreef:

>
>
>> Juha Manninen schrieb:
>> const
>>   a: array of string = ('aaa', 'bbb', 'ccc');
>>
>> It looks like a dynamic array, but obviously is not dynamic because it
>> is constant. :-)
>> The indexing would start from 0 like with dynamic arrays.
>> It would not break the existing syntax and would be very intuitive.
>
> I would appreciate such a change. I often have static lists in my programs,
> which need to be changed from time to time.
>

I suggest you add an entry on
http://wiki.lazarus.freepascal.org/Bounties#Multi-platform_bounties, so
we know how much you would appreciate it.

Vincent
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Const Array Length (was Dynamic Array Length)

Frank Peelo
In reply to this post by Rainer Stratmann
Rainer Stratmann wrote:
> Am Saturday 02 January 2010 16:34:57 schrieb JoshyFun:
>> a: array[1..] or string = ('aaa', 'bbb', 'ccc');
>>
>
> That would be perfect.
> Then you can automatically calculate the amount:
> amax = sizeof( a ) div sizeof( a[ 1 ] );

If this syntax was in Pascal, would you use
High(a)-Low(a)+1
rather than sizeof()?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Const Array Length (was Dynamic Array Length)

Rainer Stratmann
Am Sunday 03 January 2010 13:03:37 schrieb Frank Peelo:

> Rainer Stratmann wrote:
> > Am Saturday 02 January 2010 16:34:57 schrieb JoshyFun:
> >> a: array[1..] or string = ('aaa', 'bbb', 'ccc');
> >
> > That would be perfect.
> > Then you can automatically calculate the amount:
> > amax = sizeof( a ) div sizeof( a[ 1 ] );
>
> If this syntax was in Pascal, would you use
> High(a)-Low(a)+1
> rather than sizeof()?

I think high and low has an other meaning.
I C this works. There I have the idea from.

Best regards, Rainer

> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Const Array Length (was Dynamic Array Length)

Jonas Maebe-2
In reply to this post by Frank Peelo

On 03 Jan 2010, at 13:03, Frank Peelo wrote:

> Rainer Stratmann wrote:
>> Am Saturday 02 January 2010 16:34:57 schrieb JoshyFun:
>>> a: array[1..] or string = ('aaa', 'bbb', 'ccc');
>>>
>> That would be perfect.
>> Then you can automatically calculate the amount:
>> amax = sizeof( a ) div sizeof( a[ 1 ] );
>
> If this syntax was in Pascal, would you use
> High(a)-Low(a)+1
> rather than sizeof()?

Even easier is length(a) in this case. As a bonus, low/high/length won't break when you use a dynamic or open array, while C-style sizeof calculations will break in such cases.


Jonas_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Const Array Length (was Dynamic Array Length)

Jonas Maebe-2
In reply to this post by Rainer Stratmann

On 03 Jan 2010, at 13:12, Rainer Stratmann wrote:

> Am Sunday 03 January 2010 13:03:37 schrieb Frank Peelo:
>> Rainer Stratmann wrote:
>>> Then you can automatically calculate the amount:
>>> amax = sizeof( a ) div sizeof( a[ 1 ] );
>>
>> If this syntax was in Pascal, would you use
>> High(a)-Low(a)+1
>> rather than sizeof()?
>
> I think high and low has an other meaning.

You might want to read the manual: http://www.freepascal.org/docs-html/ref/refsu14.html

> I C this works. There I have the idea from.

Pascal has better ways to deal with array bounds.


Jonas_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Const Array Length (was Dynamic Array Length)

Rainer Stratmann
Am Sunday 03 January 2010 13:19:39 schrieb Jonas Maebe:

> On 03 Jan 2010, at 13:12, Rainer Stratmann wrote:
> > Am Sunday 03 January 2010 13:03:37 schrieb Frank Peelo:
> >> Rainer Stratmann wrote:
> >>> Then you can automatically calculate the amount:
> >>> amax = sizeof( a ) div sizeof( a[ 1 ] );
> >>
> >> If this syntax was in Pascal, would you use
> >> High(a)-Low(a)+1
> >> rather than sizeof()?
> >
> > I think high and low has an other meaning.
>
> You might want to read the manual:
> http://www.freepascal.org/docs-html/ref/refsu14.html

Ok, I read it before.

> > I C this works. There I have the idea from.
>
> Pascal has better ways to deal with array bounds.

But at this moment it is not possible to make a const array with auto
calculated range ( [ 1..] ). It would be easy to implement, I think.

>
> Jonas_______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Const Array Length (was Dynamic Array Length)

Jürgen Hestermann


> Rainer Stratmann schrieb:
>> Pascal has better ways to deal with array bounds.
> But at this moment it is not possible to make a const array with auto
> calculated range ( [ 1..] ). It would be easy to implement, I think.

But these things are not related. Calculating array bounds should
not be dependend on the array type (and its declaration).

When determining the number of elements in an array I therefore also think
that sizeof() or High()-Low()+1 would be the best. And that should also
be the case for constant arrays with auto size calculation (if someone
finds the time to implement it and if it can be done easily).
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal