ArraySize() reports upper bound, not size, of Array

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

ArraySize() reports upper bound, not size, of Array

Post by Blue »

Code: Select all

Dim MyArray.l(1)                ; We have 2 elements
Debug "ArraySize(myArray()) = " + ArraySize(myArray())  ; reports 1, which is the upper bound, not the size

Dim anotherArray.l(0)           ; We have 1 element, therefore size of the array is 1
Debug "ArraySize(anotherArray()) = " + ArraySize(anotherArray())    ; the function reports 0 ! should be 1
More a misnomer, an error in logic, than a bug, really.
Still, for the sake of clarity, this function should be renamed ArrayLimit() or ArrayLast() or ArrayBound() or anything but ArraySize().
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: ArraySize() reports upper bound, not size, of Array

Post by NicTheQuick »

This change would break a lot of old code.
But anyway, you're right in my opinion.

+1
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: ArraySize() reports upper bound, not size, of Array

Post by mk-soft »

NicTheQuick wrote: Sat Mar 25, 2023 12:46 pm This change would break a lot of old code.
But anyway, you're right in my opinion.

+1
True. But it's not a bug.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
AZJIO
Addict
Addict
Posts: 1312
Joined: Sun May 14, 2017 1:48 am

Re: ArraySize() reports upper bound, not size, of Array

Post by AZJIO »

Size can be interpreted in different ways, for example, a size indicator. The problem is that if you get the real size, then in the loop you will always have to specify ArraySize() - 1
Same friendly names ListSize and MapSize. It would be strange for an array to look for another function name.
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: ArraySize() reports upper bound, not size, of Array

Post by Blue »

Thanks, NicTheQuick, for getting the point.
Of course, a change to such a basic function would break a lot of code.

Specifically however, as mk-soft reminded us, it’s not a software bug. As I pointed out in my original post, it’s simply a poorly named function. Thus, a logical bug.

As for the notion that size can be interpreted in many different ways, that’s just convenient whitewash. ListSize() correctly reports the size of a list, MapSize() does the same for mapped data, but ArraySize() does something different than its name promises. So, in a business that thrives on rules and syntax and logics, I find it strange and distracting.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: ArraySize() reports upper bound, not size, of Array

Post by skywalk »

I agree it is painful for my brain to remember such contradictions.
But Macro()'s can soften the blow if you must.

It is still less onerous than [CR+LF] vs [LF] or why we still have [in] vs [cm] in 2023 :evil:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: ArraySize() reports upper bound, not size, of Array

Post by Blue »

skywalk wrote: Sat Mar 25, 2023 2:37 pm […]
But Macro()'s can soften the blow if you must.
excellent :idea: … yes, macros are wonderful and super useful, especially the resident variety; with them, we can rewrite — or simply enhance — the whole language. But they can make debugging hellish… :D
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: ArraySize() reports upper bound, not size, of Array

Post by STARGÅTE »

If there will be the day when this is changed, I would also change the size specification in Dim and ReDim, to be consistent.
Then, Dim Integers(10), will create an array from index 0 to 9 (similar to static arrays).
Further, then we can use Dim Integers(0) to create an array with zero elements, which is not possible at the moment.
Currently, you need a separat variable to differentiate between a non-initialized array with ArraySize() = -1, an empty array with ArraySize() = 0 and an array of one element with ArraySize() = 0
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: ArraySize() reports upper bound, not size, of Array

Post by Blue »

STARGÅTE wrote: Sat Mar 25, 2023 2:52 pm Currently, you need a separat variable to differentiate between a non-initialized array with ArraySize() = -1, an empty array with ArraySize() = 0 and an array of one element with ArraySize() = 0
Ahhh… STARGÅTE. Still daydreaming, are you ? :shock: To be young and idealist, those were the days, weren’t they ? :wink:
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
AZJIO
Addict
Addict
Posts: 1312
Joined: Sun May 14, 2017 1:48 am

Re: ArraySize() reports upper bound, not size, of Array

Post by AZJIO »

Imagine that in order to return the correct size to you, you need to add one, and when you receive it, you subtract one. You have made a useless action.
I was once surprised that the array index starts from zero. But this is an existing thing, it cannot be zero. But then I got used to it, since many of the indexes of the ListIconGadget() item and other functions also start from 0. So if you start from 1, you would have to constantly subtract one to enumerate the elements. The character/cursor position in the editor can be zero-based, which is not logical. Having received the position, we need to take into account where the countdown starts, from 1 or from 0.
Why Arr(0) but "Procedure FuncName(Array Arr(1))"
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: ArraySize() reports upper bound, not size, of Array

Post by Blue »

AZJIO wrote: Sat Mar 25, 2023 6:59 pm Imagine that …
Are you trying to convince someone of something here ? I’m not sure I follow what your explanations, nor am I sure you’re getting the point.
Just the same, though, thanks for the input.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: ArraySize() reports upper bound, not size, of Array

Post by Demivec »

Blue wrote: Sat Mar 25, 2023 9:12 pm
AZJIO wrote: Sat Mar 25, 2023 6:59 pm Imagine that …
Are you trying to convince someone of something here ? I’m not sure I follow what your explanations, nor am I sure you’re getting the point.
Just the same, though, thanks for the input.
I think that ArraySize() is named correctly. It returns the dimensioned size, not the number of elements. I don't think it should be changed. I was bitten by this feature recently when i was converting code from another language whose size function counted the number of elements instead of returning the dimensioned size.

The missing ability to determine whether an array is null | empty mentioned by STARGÅTE is a byproduct of PureBasic's foundation. That is to say, it had been there from the start and I would guess it is not an easy thing to change. Even so, I agree with the thought that it would be helpful to have that functionality at times but I think it is more or less set in stone at this point.
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: ArraySize() reports upper bound, not size, of Array

Post by Blue »

Demivec wrote: Sat Mar 25, 2023 9:40 pm… it is more or less set in stone at this point.
Agreed. And we’re all used to it. The experience you relate attests to that. Still, the fact remains, however, that better consistency would be desirable, and appreciated, in the logic department.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
AZJIO
Addict
Addict
Posts: 1312
Joined: Sun May 14, 2017 1:48 am

Re: ArraySize() reports upper bound, not size, of Array

Post by AZJIO »

Blue wrote: Sat Mar 25, 2023 9:12 pm I’m not sure I follow what your explanations
Yes, Great, Right

Code: Select all

Define Dim Arr(5)
Define i

Arr(0) = 0
Arr(1) = 1
Arr(2) = 2
Arr(3) = 3
Arr(4) = 4
Arr(5) = 5

For i = 0 To ArraySize(Arr())
	Debug Arr(i)
Next
no, bad, wrong

Code: Select all

Macro ArraySize2(fuf)
	ArraySize(fuf) + 1
EndMacro

Define Dim Arr(5)
Define i

Arr(0) = 0
Arr(1) = 1
Arr(2) = 2
Arr(3) = 3
Arr(4) = 4
Arr(5) = 5

For i = 0 To ArraySize2(Arr()) - 1
	Debug Arr(i)
Next
How much of the essence is distorted by the double translation? (DeepL)
Is the essence strongly distorted after a double translation? (Google)
Image
Jeff8888
User
User
Posts: 38
Joined: Fri Jan 31, 2020 6:48 pm

Re: ArraySize() reports upper bound, not size, of Array

Post by Jeff8888 »

I noted that the help file for arraysize says Arraysize returns the array dimension and not the array size.
Post Reply