Sign in Join

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

Mailing lists / fpc-pascal / 2009-12

Dynamic Array Length

18 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

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

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.

Reply by email

Anthony Walter
to Rainer Stratmann

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

Reply by email

Rainer Stratmann
to Anthony Walter

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

Anthony Walter wrote 45 lines
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

Reply by email

Rainer Stratmann
to Anthony Walter
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.

But it would be a step to more userfriendly if the compiler calculates it automatically. I would be possible.

Reply by email

Juha Manninen
to Anthony Walter: No, the compiler will not fill in the array length based on your element list. It does…
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

Reply by email

JoshyFun
to Juha Manninen

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

Reply by email

Jürgen Hestermann
to Juha Manninen: In most cases it would be better if the compiler counted the number of elements. Only in…
Show quoted text 8 lines
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.

Reply by email

Juha Manninen
to JoshyFun: JM> In most cases it would be better if the compiler counted the number of JM> elements.…
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

Reply by email

Rainer Stratmann
to JoshyFun: JM> In most cases it would be better if the compiler counted the number of JM> elements.…
JoshyFun wrote
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.

Reply by email

Vincent Snijders
to Jürgen Hestermann: I would appreciate such a change. I often have static lists in my programs, which need to…
Jürgen Hestermann wrote 14 lines
> 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

Reply by email

Rainer Stratmann
to Frank Peelo
Frank Peelo wrote 11 lines
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 - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Jonas Maebe
to Frank Peelo: If this syntax was in Pascal, would you use High(a)-Low(a)+1 rather than sizeof()?
Frank Peelo wrote 11 lines
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

Reply by email

Jonas Maebe
to Rainer Stratmann: I think high and low has an other meaning. I C this works. There I have the idea from.…
Rainer Stratmann wrote 10 lines
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

Reply by email

Rainer Stratmann
to Jonas Maebe
Jonas Maebe wrote 14 lines
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 - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Jürgen Hestermann
to Rainer Stratmann
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).

Reply by email