Release: 20.04
FreeBASIC Compiler - Version 1.09.0 (2022-01-01), built for linux-x86_64 (64bit)
Copyright (C) 2004-2021 The FreeBASIC development team.
Code: Select all
dim shared as sub() ptr pbfun
sub pm_boxfun(cbox as long, bxinit as sub() ptr)
' table of pointers to init functions for specific boxes
static as sub(cbox as long) subtable(any)
dim as long result, xix, pndx
if (ubound(subtable) = -1) then ' initial run
' yes: I always have an unused 0 item - my convention
redim subtable(0 to 1)
else
pndx = ubound(subtable)
if cbox = 0 then ' new box request
xix = 1
do
if subtable(xix) = 0 then ' found empty slot
result = xix
subtable(xix) = bxinit
exit do
else
xix += 1
end if
if xix = pndx then ' array full
' always at least one empty slot at end of table
redim preserve subtable(0 to xix + 1)
subtable(xix) = bxinit
result = xix
exit do
end if
if result <> 0 then exit do
loop
end if
end if
if cbox < 0 then ' delete a box
subtable(abs(cbox)) = 0
end if
pbfun = @subtable(0)
' < assign xix to calling box data >
end sub
Essentially the same structure works for other variables and UDTs.fbc -exx test2.bas
test2.bas(20) warning 4(2): Suspicious pointer assignment
test2.bas(28) warning 4(2): Suspicious pointer assignment
test2.bas(41) warning 4(2): Suspicious pointer assignment
test2.c:20:2: error: unknown type name ‘tmp$3’
20 | tmp$3* DATA;
| ^~~~~
test2.c:21:2: error: unknown type name ‘tmp$3’
21 | tmp$3* PTR;
| ^~~~~
test2.c: In function ‘PM_BOXFUN’:
test2.c:72:49: warning: initialization of ‘int *’ from incompatible pointer type ‘void (**)(int32)’ {aka ‘void (**)(int)’} [-Wincompatible-pointer-types]
72 | static struct $8FBARRAY1IPFviEE SUBTABLE$1 = { (tmp$3*)0ull, (tmp$3*)0ull, 0ll, 8ll, 1ll, 17ll, { } };
| ^
test2.c:72:49: note: (near initialization for ‘SUBTABLE$1.DATA’)
test2.c:72:63: warning: initialization of ‘int *’ from incompatible pointer type ‘void (**)(int32)’ {aka ‘void (**)(int)’} [-Wincompatible-pointer-types]
72 | static struct $8FBARRAY1IPFviEE SUBTABLE$1 = { (tmp$3*)0ull, (tmp$3*)0ull, 0ll, 8ll, 1ll, 17ll, { } };
| ^
test2.c:72:63: note: (near initialization for ‘SUBTABLE$1.PTR’)
How could this be fixed, or will the compiler just not do it?
david