sub() ptr - why does this not work?

General FreeBASIC programming questions.
Post Reply
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

sub() ptr - why does this not work?

Post by speedfixer »

Description: Ubuntu 20.04.4 LTS
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
result:
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’)
Essentially the same structure works for other variables and UDTs.
How could this be fixed, or will the compiler just not do it?

david
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: sub() ptr - why does this not work? - solved

Post by speedfixer »

Do I need to bury the sub pointer in a UDT to get it handled?
(I'll try that.)

edit:
OK. That worked, though I think it should have worked *somewhat* like I coded above.
I tried variations on that above; none worked.

david
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: sub() ptr - why does this not work?

Post by srvaldez »

hello
you declare your sub() variables differently, how about this

Code: Select all

dim shared as sub(cbox as long) ptr pbfun

sub pm_boxfun(cbox as long, bxinit as sub(cbox as long) ptr)
' table of pointers to init functions for specific boxes

static as sub(cbox as long) ptr 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
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: sub() ptr - why does this not work?

Post by fxm »

Remark:
'as sub(cbox as long)' already refers to procedure pointers.
So 'as sub(cbox as long) ptr' then refers to pointers to procedure pointers. Is this your will ?

See Pointers to Procedures documentation page.

Example:

Code: Select all

Sub PrintHalve (Byval i As Integer)
    Print i / 2
End Sub
Dim As Sub(Byval As Integer) pph = @PrintHalve

Sub PrintTriple (Byval i As Integer)
    Print i * 3
End Sub
Dim As Sub(Byval As Integer) ppt = @PrintTriple


Sub OperationPP (Byval i As Integer, Byval ps As Sub(Byval As Integer))
    '' second parameter is a procedure pointer
    ps(i)
End Sub

Sub OperationP2PP Overload (Byval i As Integer, Byval pps As Sub(Byval As Integer) Ptr)
    '' second parameter is a pointer to procedure pointer
    (*pps)(i)
End Sub


OperationPP(6, pph)
OperationPP(6, ppt)
Print
OperationP2PP(6, @pph)
OperationP2PP(6, @ppt)

Sleep
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: sub() ptr - why does this not work?

Post by speedfixer »

Thanks for the code reviews.

I want to table the functions so I can call them automatically by the calling index - would be the specific box, in this case.

Each screen zone/box would have a different set of utility functions.
The box identifier - long - would be thrown at generic create/construct/update/kill functions.
Look up the needed function for that box in the table - do it.

I already have a library that does all this. I just wanted to try a different approach.

I have code that compiles and runs ok now.
I was just having difficulty about when pointer, when sub only. Too much is going on around me and concentration is difficult, sometimes. Age doesn't help.

david
Post Reply