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.
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
Dim Array() As Integer
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.