About:
For more information, see discussion at:
https://www.freebasic.net/forum/viewtopic.php?t=33246
1) Bug when 'identifier' is a var-len array and 'DataType' has a constructor
For 'DataType' without default constructor, the 'DataType' instance data is not initialized whatever the 'identifier': simple variable or UDT or all array types.
For 'DataType' with a default constructor, the 'DataType' instance data should not be initialized and default constructor should not be called.
But in a wrong way, when the 'identifier' is a var-len array (declared with '[Re]Dim As DataType array() = Any' or 'Redim As DataType array(N) = Any'), the instance data is initialized and default constructor is called (no problem for a simple variable or an UDT instance or a fix-len array).
Code to test this wrong behavior (if 'ctor' is defined, the UDT has a default constructor):
#define ctor
#ifdef ctor
Print "UDT HAS A DEFAULT CONSTRUCTOR"
#else
Print "UDT HAS NO DEFAULT CONSTRUCTOR"
#endif
Print "------------------------------"
Print
Type UDT
Dim As Byte b
#ifdef ctor
Declare Constructor()
#endif
End Type
#ifdef ctor
Constructor UDT()
Print "ctor",
End Constructor
#endif
Print "'Dim As UDT u1' :"
Dim As UDT u1
Print u1.b
Print
Print "'Dim As UDT u2 = Any' :"
Dim As UDT u2 = Any
Print u2.b
Print
Print "'Dim As UDT au1(0 To 2)' :"
Dim As UDT au1(0 To 2)
Print au1(0).b, au1(1).b, au1(2).b
Print
Print "'Dim As UDT au2(0 To 2) = Any' :"
Dim As UDT au2(0 To 2) = Any
Print au2(0).b, au2(1).b, au2(2).b
Print
Print "'Redim As UDT au3(0 To 2)' :"
Redim As UDT au3(0 To 2)
Print au3(0).b, au3(1).b, au3(2).b
Print
Print "'Redim As UDT au4(0 To 2) = Any' :"
Redim As UDT au4(0 To 2) = Any
Print au4(0).b, au4(1).b, au4(2).b
Print
Print "'Dim As UDT au5() : Redim au5(0 To 2)' :"
Dim As UDT au5() : Redim au5(0 To 2)
Print au5(0).b, au5(1).b, au5(2).b
Print
Print "'Dim As UDT au6() = Any : Redim au6(0 To 2)' :"
Dim As UDT au6() = Any : Redim au6(0 To 2)
Print au6(0).b, au6(1).b, au6(2).b
Print
Sleep
UDT HAS NO DEFAULT CONSTRUCTOR
------------------------------
'Dim As UDT u1' :
0 <= OK
'Dim As UDT u2 = Any' :
-124 <= OK
'Dim As UDT au1(0 To 2)' :
0 0 0 <== OK
'Dim As UDT au2(0 To 2) = Any' :
32 106 30 <== OK
'Redim As UDT au3(0 To 2)' :
0 0 0 <== OK
'Redim As UDT au4(0 To 2) = Any' :
-96 40 -71 <== OK
'Dim As UDT au5() : Redim au5(0 To 2)' :
0 0 0 <== OK
'Dim As UDT au6() = Any : Redim au6(0 To 2)' :
-96 40 -71 <== OK
UDT HAS A DEFAULT CONSTRUCTOR
------------------------------
'Dim As UDT u1' :
ctor 0 <== OK
'Dim As UDT u2 = Any' :
-124 <== OK
'Dim As UDT au1(0 To 2)' :
ctor ctor ctor 0 0 0 <== OK
'Dim As UDT au2(0 To 2) = Any' :
8 -5 47 <== OK
'Redim As UDT au3(0 To 2)' :
ctor ctor ctor 0 0 0 <== OK
'Redim As UDT au4(0 To 2) = Any' :
ctor ctor ctor 0 0 0 <== NOK : BUG
'Dim As UDT au5() : Redim au5(0 To 2)' :
ctor ctor ctor 0 0 0 <== OK
'Dim As UDT au6() = Any : Redim au6(0 To 2)' :
ctor ctor ctor 0 0 0 <== NOK : BUG
2) Bug when 'DataType' is an UDT containing a var-len string member
'Dim As String s = Any' is forbidden (OK: no string character data, and not initializing the descriptor would be an aberration).
But 'Dim As UDT u = Any' is allowed even if the UDT contains a var-len string member (therefore the string descriptor of the member is not initialized).
Two possible fixes:
Type UDT
Dim As Integer I = 123
Dim As String S = "FreeBASIC"
End Type
Dim As UDT u = Any
Clear u.S, 0, 3 * Sizeof(Integer) ' Equivalent of this to add if 'Any'
I started the topic "Validity/Behavior of 'ANY' initializer versus datatypes with default constructor":
https://www.freebasic.net/forum/viewtopic.php?t=33278
to discuss and decide on the usage and behavior of 'Any' versus objects with constructors.
Currently, the behavior of 'Any' varies greatly, depending on the data type and declaration type.
Therefore, processing of this bug report is suspended for the time being.
Last edit: fxm (freebasic.net) 2025-08-09