Array Descriptor (split from Wiki Improvements)

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

Re: Array Descriptor (split from Wiki Improvements)

Post by MrSwiss »

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
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array Descriptor (split from Wiki Improvements)

Post by fxm »

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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Array Descriptor (split from Wiki Improvements)

Post by MrSwiss »

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

Re: Array Descriptor (split from Wiki Improvements)

Post by fxm »

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

Re: Array Descriptor (split from Wiki Improvements)

Post by MrSwiss »

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

Re: Array Descriptor (split from Wiki Improvements)

Post by fxm »

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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Array Descriptor (split from Wiki Improvements)

Post by MrSwiss »

.. 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
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array Descriptor (split from Wiki Improvements)

Post by fxm »

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: 225
Joined: Apr 11, 2011 22:22

Re: Array Descriptor (split from Wiki Improvements)

Post by gothon »

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 ... ily-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: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Array Descriptor (split from Wiki Improvements)

Post by D.J.Peters »

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
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array Descriptor (split from Wiki Improvements)

Post by fxm »

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: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Array Descriptor (split from Wiki Improvements)

Post by dodicat »

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
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array Descriptor (split from Wiki Improvements)

Post by fxm »

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: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Array Descriptor (split from Wiki Improvements)

Post by dodicat »

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: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: Array Descriptor (split from Wiki Improvements)

Post by Juergen Kuehlwein »

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
Post Reply