(flags and &h0000000f) provides the number of dimensions allocated in the descriptor (and not the number of dimensions of the array which is the 'dimensions' field).dodicat wrote:How on earth does this work?
The descriptor memory is allocated at the first array declaration.
When the array is first declared without dimensions ('[re]dim array()'), a descriptor size for 8 dimensions (the maximum) is allocated.
Then when the array is re-declared with an explicit number of dimensions ('redim array(0)' for example), the descriptor is not reallocated (only the 'dimensions' field is set and the end of the descriptor memory may become useless).
(flags and &h00000010) provides a flag which signifies that the descriptor has been allocated with a fixed number of dimensions (=> the one of the array)
( see FBARRAY (array descriptor structure and access) / paragraph 'Description' / FBARRAY.flags) )
Another code variant not using 'goto':
Code: Select all
sub align(s() as single,n as long,f as long=0)
do
redim as integer b(f) 'get some granularity
redim s(n+f)
if cast(uinteger,@s(0)) mod 32 =0 then f+=1:continue do
if cast(uinteger,@s(0)) mod 16 <>0 then f+=1:continue do
var i=cast(uinteger,@s(0))
redim preserve s(n)
if cast(uinteger,@s(0))<> i then f+=1: else exit do
loop
end sub