Recent

Author Topic: Constant dynamic array  (Read 6836 times)

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Constant dynamic array
« on: January 18, 2022, 02:35:19 am »
Why doesn't Length or High work with constant dynamic array like this when target is AVR:
Code: Pascal  [Select][+][-]
  1. const test : array of byte = (1, 2, 3, 4, 10);

Length(test) gives 1 and High(test) gives 0.
Accessing members of this array works normal.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Constant dynamic array
« Reply #1 on: January 18, 2022, 10:00:15 am »
Does it work with a fixed length array? After all it is a typed const.
What is the state of {$J+/-}, may be writable consts should be off?
It may be deemed that the overhead of dynamic arrays was too big for AVR?

E.g does it work with:
Code: Pascal  [Select][+][-]
  1. const test : array[0..4] of byte = (1, 2, 3, 4, 10);
Or maybe:
Code: Pascal  [Select][+][-]
  1. {$push}{$J-}
  2. const test : array of byte = (1, 2, 3, 4, 10);
  3. {$pop}
I am curious if those two would work, but atm I have no AVR setup available at my current location.
« Last Edit: January 18, 2022, 10:22:12 am by Thaddy »
Specialize a type, not a var.

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: Constant dynamic array
« Reply #2 on: January 18, 2022, 02:24:47 pm »
I have {$J+} so I can have writeable constants.

This code:
Code: Pascal  [Select][+][-]
  1. const test1 : array[0..4] of byte = (1, 2, 3, 4, 10);
  2. {$push}{$J-}
  3. const test2 : array of byte = (1, 2, 3, 4, 10);
  4. var test3 : array of byte = (1, 2, 3, 4, 10);
  5. {$pop}
  6. var test4 : array of byte = (1, 2, 3, 4, 10);

Gives lengths:
Length(test1) = 5
Length(test2) = 1
Length(test3) = 1
Length(test4) = 1

So, it works with fixed length, but not with dynamic arrays.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Constant dynamic array
« Reply #3 on: January 18, 2022, 03:19:38 pm »
 :) I am not surprised... You need small code? system is obeying. At least it seems it is not a bug.
Be careful what you wish for....
In general, do not use {$I+} with AVR or any other micro with limited resources.

I will test when I am at home. Still curious.
« Last Edit: January 18, 2022, 03:29:09 pm by Thaddy »
Specialize a type, not a var.

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Constant dynamic array
« Reply #4 on: January 18, 2022, 04:36:28 pm »
At least it seems it is not a bug.

How so? It is in contrast with documentation, isn't it then a bug by definition?
The docs say (link):
Quote
For dynamic or static arrays, the function returns the number of elements in the array.

No exceptions.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Constant dynamic array
« Reply #5 on: January 18, 2022, 05:44:59 pm »
No exceptions.
Well, I think here an exception is warranted if it draws in too much code.
I am always struggling to get the most of the micro platforms.
Mainly because I am lazy and forgot the basics.
But I agree the documentation should make users aware of such issues,
(imho writable consts are particularly useful for mp's,but dynamic array as consts is - unintentional - programmer error)

Let's see what the Dev's say. Otherwise report bug.
« Last Edit: January 18, 2022, 05:58:02 pm by Thaddy »
Specialize a type, not a var.

Laksen

  • Hero Member
  • *****
  • Posts: 724
    • J-Software
Re: Constant dynamic array
« Reply #6 on: January 18, 2022, 09:05:10 pm »
I don't have a working compiler handy currently, but I imagine that the problem could be lack of support for dynamic arrays, pointers being initialized to flash storage, or the program not crashing because you don't have a working heapmgr when the arrays are allocated (if they are allocated). Either way, a bug for sure

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Constant dynamic array
« Reply #7 on: January 18, 2022, 09:40:31 pm »
I don't have a working compiler handy currently, but I imagine that the problem could be lack of support for dynamic arrays, pointers being initialized to flash storage, or the program not crashing because you don't have a working heapmgr when the arrays are allocated (if they are allocated). Either way, a bug for sure

Much more trivial: one part of the compiler assumes that the High value stored in the array is 8-bit the other part assumes 16-bit and that obviously doesn't go hand in hand that well. ;) (in this case only the high part of the 16-bit High value is loaded which is 0 and thus High returns 0 and Length returns 1)

Regarding the state of dynamic arrays on AVR in general:
The functions that are used for dynamically allocating a dynamic array are disabled by default on AVR (e.g. SetLength), however these can be enabled by compiling the RTL with a suitable configuration as well as providing the tiny heap.
The Length and High however are intrinsics that are not handled by the RTL, but by the compiler itself and thus are available always just like the ability to declare a dynamic array is always available.
Declaring constants might be one of the few uses dynamic arrays have on AVR, though you'll always have 8-Byte overhead for each constant declared such compared to a static array of the same size (2 Byte reference count, 2 Byte size and 2 Byte Pointer to the array data).

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: Constant dynamic array
« Reply #8 on: January 18, 2022, 10:13:52 pm »
I will use fixed length array, it is not a big deal. It's just that it would be more convenient, when adding or removing data from array, not to worry about adjusting bounds.

And this also doesn't work:
Code: Pascal  [Select][+][-]
  1. test : array of byte = (1, 2, 3, 4, 10); section '.progmem';

For this only $4e02 was written in flash memory.

Probably same reasons as PascalDragon explained.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Constant dynamic array
« Reply #9 on: January 18, 2022, 11:44:32 pm »
I've fixed the issue with the Length and High in c59b6a5d.

And this also doesn't work:
Code: Pascal  [Select][+][-]
  1. test : array of byte = (1, 2, 3, 4, 10); section '.progmem';

For this only $4e02 was written in flash memory.

Probably same reasons as PascalDragon explained.

Not the same reason. It's instead that only the pointer to the array is stored in the flash memory, but not the array data itself. Please report it as a bug.

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: Constant dynamic array
« Reply #10 on: January 19, 2022, 02:33:18 am »
I've fixed the issue with the Length and High in c59b6a5d.

Thank you.

Quote from: PascalDragon
Not the same reason. It's instead that only the pointer to the array is stored in the flash memory, but not the array data itself. Please report it as a bug.

Reported: https://gitlab.com/freepascal.org/fpc/source/-/issues/39534

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Constant dynamic array
« Reply #11 on: January 19, 2022, 09:53:13 am »
Quote from: PascalDragon
Not the same reason. It's instead that only the pointer to the array is stored in the flash memory, but not the array data itself. Please report it as a bug.

Reported: https://gitlab.com/freepascal.org/fpc/source/-/issues/39534

Thank you. Please note however that even if it's fixed it will probably need ccrause's merge request so that the compiler can transparently access data stored in e.g. EEPROM so that it can be used nicely.

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: Constant dynamic array
« Reply #12 on: January 19, 2022, 08:05:36 pm »
Please note however that even if it's fixed it will probably need ccrause's merge request so that the compiler can transparently access data stored in e.g. EEPROM so that it can be used nicely.

Yes, of course.
Until then I'll use arrays with fixed boundaries and copy rows to RAM as needed.

 

TinyPortal © 2005-2018