Array Descriptor (split from Wiki Improvements)

Forum for discussion about the documentation project.
MrSwiss
Posts: 3221
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Array Descriptor (split from Wiki Improvements)

Postby MrSwiss » Aug 19, 2019 12:47

fxm wrote:I hope this will be an Integer (32/64 bits) like all other fields, so 32 bits useful?
No, no, no --> anyting but Integer, in structs we should only consider fixed size variables as DWORD (aka: ULong).
Otherwise, the struct-size is different, in the 32/64 compilers (a really bad idea, me thinks).
Added:
Bitfield (as the intention seems to be) should be "defined" in a static way.
fxm
Posts: 9126
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array Descriptor (split from Wiki Improvements)

Postby fxm » Aug 19, 2019 14:43

Anyway, as this type of structure (descriptors) already contains pointers and most of the other fields are information linked to available addressing, this will be done naturally with 32/64 bits.
MrSwiss
Posts: 3221
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Array Descriptor (split from Wiki Improvements)

Postby MrSwiss » Aug 19, 2019 14:54

Whether there are pointers or not is irrelevant, when "bitfield" is the issue.
The compiler takes care of eventually needed "padding".
fxm
Posts: 9126
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array Descriptor (split from Wiki Improvements)

Postby fxm » Aug 19, 2019 15:40

In any way, the final decision is up to the developer (Jeff).
MrSwiss
Posts: 3221
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Array Descriptor (split from Wiki Improvements)

Postby MrSwiss » Aug 19, 2019 15:44

Why do you always feel the need, to state the obvious?
(wanting the "last word" or something else?)
fxm
Posts: 9126
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array Descriptor (split from Wiki Improvements)

Postby fxm » Aug 19, 2019 16:08

MrSwiss wrote:Why do you always feel the need, to state the obvious?

I do not feel that other users view most of my posts and articles and documentation pages as obvious information.
MrSwiss
Posts: 3221
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Array Descriptor (split from Wiki Improvements)

Postby MrSwiss » Aug 19, 2019 16:15

.. the final decision is up to the developer (Jeff).
That is obvious ...
The remainder I consider another "side tracking attempt".
You do everything, to avoid answering "the real question(s)".
Last edited by MrSwiss on Aug 19, 2019 16:26, edited 1 time in total.
fxm
Posts: 9126
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array Descriptor (split from Wiki Improvements)

Postby fxm » Aug 19, 2019 16:26

In the present structure of array descriptor, one single field could be instanced on a shorter word (than Pointer/Integer). This is the number of dimensions (between 0 and 8). Is it better to use here a Byte instead of an Integer in the descriptor structure?
gothon
Posts: 224
Joined: Apr 11, 2011 22:22

Re: Array Descriptor (split from Wiki Improvements)

Postby gothon » Aug 19, 2019 18:11

There is actually another issue related to static arrays as parameters. It turns out that VB has a mechanism for size locking dynamic arrays, and VB can't distinguish between a size locked dynamic array as a parameter and an actual statically sized array. In ether case VB throws a run time error if an attempt to resize the array is made:

https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/error-messages/this-array-is-fixed-or-temporarily-locked

It seems VB has an array descriptor that indicates whether or not the array is size locked, and fixed length array parameters are simply passed as though they are size locked dynamic arrays. But why would VB need to size lock a dynamic array?

The answer is that when an array is resized, any references (eg pointers) to array elements are invalidated. It is possible to make references to array elements in VB with out pointers by passing the array element ByRef.

The same can be done in FB as well. Instead of forbidding an array resize, FB just lets the reference become invalid like a pointer in C.
ex:

Code: Select all

Sub Proc(ByRef Result As Integer, Ar() As Integer)
   
    Print Result  '<- Valid
   
    ReDim Ar(20)  '<- The reference is invalidated
   
    Print Result  '<- Invalid, the element may have been moved or freed
   
End Sub

Dim Array() As Integer

ReDim Array(10)

Array(1) = 5

Proc Array(1), Array()


The way VB size locks an array is probably through reference counting. It is most likely the array descriptor has a count for the number of references to the array's elements, and forbids any resizing unless the count is zero. This method has both advantages and drawbacks, and I think now is a time we might consider doing something similar in FB. If we were to do this, we would need a reference count field in addition to or instead of the new flags field.
D.J.Peters
Posts: 7810
Joined: May 28, 2005 3:28

Re: Array Descriptor (split from Wiki Improvements)

Postby D.J.Peters » Aug 19, 2019 19:53

gothon wrote:The way VB size locks an array is probably through reference counting.
Sure in VB an array is always an object also. (so far I remember)

Joshy
fxm
Posts: 9126
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array Descriptor (split from Wiki Improvements)

Postby fxm » Aug 19, 2019 20:59

Code: Select all

Sub Proc(ByRef Result As Integer, Ar() As Integer)
    Print Result  '<- Valid
   
    ReDim Preserve Ar(20)  '<- The reference is invalidated
    Print Result  '<- Invalid, the element may have been moved
   
    @Result = @Ar(1)
    Print Result  '<- Valid, the reference has been reinitialized
End Sub

Dim Array() As Integer
ReDim Array(10)
Array(1) = 5

Proc Array(1), Array()
dodicat
Posts: 5913
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Array Descriptor (split from Wiki Improvements)

Postby dodicat » Aug 19, 2019 23:48

Kinda crude array pointer and dynamic tester.
The dynamic tester fails with -O optimisations.

Code: Select all




#macro __Arrayptr__(array)
Iif(Ubound(array,0)=1,@array(Lbound(array)), _
Iif(Ubound(array,0)=2,@array(Lbound(array,1),Lbound(array,2)), _
Iif(Ubound(array,0)=3,@array(Lbound(array,1),Lbound(array,2),Lbound(array,3)), _
Iif(Ubound(array,0)=4,@array(Lbound(array,1),Lbound(array,2),Lbound(array,3),Lbound(array,4)), _
Iif(Ubound(array,0)=5,@array(Lbound(array,1),Lbound(array,2),Lbound(array,3),Lbound(array,4),Lbound(array,5)), _
Iif(Ubound(array,0)=6,@array(Lbound(array,1),Lbound(array,2),Lbound(array,3),Lbound(array,4),Lbound(array,5),Lbound(array,6)), _
Iif(Ubound(array,0)=7,@array(Lbound(array,1),Lbound(array,2),Lbound(array,3),Lbound(array,4),Lbound(array,5),Lbound(array,6),Lbound(array,7)), _
Iif(Ubound(array,0)=8,@array(Lbound(array,1),Lbound(array,2),Lbound(array,3),Lbound(array,4),Lbound(array,5),Lbound(array,6),Lbound(array,7),Lbound(array,8)),0))))))))
#endmacro

#macro setptr(datatype,fname)
Function fname(a() As datatype,Byref ret As boolean=0) As datatype Ptr
    Var z= __arrayptr__(a)
    If  z>@ret Then ret=true Else ret=false
    Return z
End Function
#endmacro


Type udt
    As Long n(100)
    As String s
End Type

'setup four datatypes
setptr(Double,doubleptr)
setptr(String,stringptr)
setptr(udt,udtptr)
setptr(Short,shortptr)

Do
    Scope
        Dim As boolean b
       
        Cls
        Locate 5
        Print "fn(ptr)","ptr","is dynamic?"
       
       
        Redim As String st(1 To Rnd*5000) 'add random memory
       
       
        If Rnd>.5 Then
            Redim As Double d(4,5 To 7)
            Print doubleptr(d(),b),@d(0,5),b,"dynamic"
        Else
            Dim As Double d(4,5 To 7)
            Print doubleptr(d(),b),@d(0,5),b,"static"
        End If
       
        If Rnd>.5 Then
            Redim As String s(-1 To 5,4,1 To 3,-3 To 4)
            Print stringptr(s(),b),@s(-1,0,1,-3),b,"dynamic"
        Else
            Dim As String s(-1 To 5,4,1 To 3,-3 To 4)
            Print stringptr(s(),b),@s(-1,0,1,-3),b,"static"
        End If
       
        If Rnd>.5 Then
            Redim As udt z(2,3)
            Print udtptr(z(),b),@z(0,0),b,"dynamic"
        Else
            Dim As udt z(2,3)
            Print udtptr(z(),b),@z(0,0),b,"static"
        End If
       
        If Rnd>.5 Then
            Redim As Short f(1 To 67,2 To 8)
            Print shortptr(f(),b),@f(1,2),b,"dynamic"
        Else
            Dim As Short f(1 To 67,2 To 8)
            Print shortptr(f(),b),@f(1,2),b,"static"
        End If
       
        Print "press a key or <esc>"
        Sleep
    End Scope
   
Loop Until Inkey=Chr(27)
 
fxm
Posts: 9126
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array Descriptor (split from Wiki Improvements)

Postby fxm » Aug 20, 2019 5:43

But the bigger problem is to distinguish between a var-len array in the heap and a fix-len array in the .BSS or .DATA section.
For example, in your Scope block, between:
Redim As Double d(4, 5 to 7) '' var-len array data in the heap
and
Static As Double d(4, 5 to 7) '' fix-len array data in the .BSS section
because we do not know by priori the value of the border between the heap and the .BSS section.
dodicat
Posts: 5913
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Array Descriptor (split from Wiki Improvements)

Postby dodicat » Aug 20, 2019 12:08

I think that when using -Wc -O3, the functions in my snippet are inlined into the scope block and the byref parameter is assigned an address on the heap. Thus causing local confusion and the wrong answer.
(Only a guess)
Juergen Kuehlwein
Posts: 210
Joined: Mar 07, 2018 13:59
Location: Germany

Re: Array Descriptor (split from Wiki Improvements)

Postby Juergen Kuehlwein » Aug 20, 2019 12:45

A flag for fixed size arrays set by the compiler will do. Currently you can redim a fixed size array inside a procedure it has been passed to, because there is no information in the array descriptor (this is what actually is passed), if an array is of fixed size or dynamic. You simply cannot tell in this case. This shouldn´t be possible, a run time error should be the result.

A dynamic array should be allowed to be redimmed (with preserve and without, even the dimension count should be allowed to change in a range from 1 to 8) everywhere and everytime. It´s up to the user to write his code in a way, that invalidated references aren´t referenced anymore!


JK

Return to “Documentation”

Who is online

Users browsing this forum: No registered users and 35 guests