Compile Errors from "var shared ...."

General FreeBASIC programming questions.
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Compile Errors from "var shared ...."

Post by Dinosaur »

Hi All

Using SNC from Joshy in a project.
But I am trying to eliminate long "waiting" delays.

Normally in my programs I break all the time consuming tasks up into steps.
Pseudo code.

Code: Select all

Sub DoTasks
Select Case StepNbr
    Case 1
        var client = NetworkClient(ServerIP,ServerPort)
        var connection = client.GetConnection()
        StepNbr += 1
    Case 2
        If connection->CanPut() = 1 Then
            Status = connection->PutData(StrPtr(TxData),Len(TxData))
            StepNbr += 1
        EndIf
    Case 3
        If connection->CanGet() Then
            nBytes = connection->GetData(buffer)
            ' etc
            StepNbr += 1
        End If
    Case 4
        'Crunch up the Data received.
End Select
End Sub
This usually results in sub mSec loop times when I call this from the Main program loop.

BUT, to do the above, the Client & Connection var's are not visible in the following cases.
So I get the error that they are not defined.

So, the other solution is to declare the var's at the start of the program.
var shared client = NetworkClient(ServerIP,ServerPort) and then just referring to
the client in the DoTasks Sub.

Then I get really weird errors.
"/usr/local/bin/fbc" -b "/home/barfresh/projects/TPLink/tplink.bas" -v

/home/barfresh/projects/TPLink/tplink.c: In function ‘_GLOBAL__I’:
/home/barfresh/projects/TPLink/tplink.c:1335:2: error: stray ‘\20’ in program
__builtin_memset( &~O, 0, 24ll );
^
/home/barfresh/projects/TPLink/tplink.c:1335:2: error: stray ‘\1’ in program
/home/barfresh/projects/TPLink/tplink.c:1335:23: error: ‘O’ undeclared (first use in this function)
__builtin_memset( &~O, 0, 24ll );
^
/home/barfresh/projects/TPLink/tplink.c:1335:23: note: each undeclared identifier is reported only once for each function it appears in
/home/barfresh/projects/TPLink/tplink.c:1336:2: error: stray ‘\20’ in program
FBSTRING* vr$2 = fb_StrAssign( (void*)&~O, -1ll, (void*)"192.168.1.145", 14ll, 0 );
^
And lots more.
Using FreeBASIC Compiler - Version 1.06.0 (02-17-2019), built for linux-x86_64 (64bit)
It looks like it never even got to call the compiler.
Can someone please shed some light on this or suggest an alternate approach.

Regards
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Compile Errors from "var shared ...."

Post by fxm »

IMHO, when you use 'Shared', you must separate declaration and initialization because the expression on the right is neither constant nor evaluable before the program starts.
You can not do it with 'Var'.
Try with 'Dim' (2 separate lines):
Dim Shared As Typeof(NetworkClient) client
client = NetworkClient(ServerIP,ServerPort)
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Compile Errors from "var shared ...."

Post by Dinosaur »

Hi All

fxm thanks for the response.
That creates an error:
error 183: TYPE or CLASS has no default constructor in 'Dim Shared as TypeOf(NetworkClient) Client'
Regards
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Compile Errors from "var shared ...."

Post by fxm »

Perhaps by using a shared pointer:
- In the main code:
Dim Shared As NetworkClient Ptr pclient
- In the Sub:
pclient = New NetworkClient(ServerIP,ServerPort)

(then 'pclient->x' to access the field 'x')
Last edited by fxm on Sep 25, 2019 18:20, edited 1 time in total.
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Compile Errors from "var shared ...."

Post by Dinosaur »

Hi All

fxm that looks promising, but with a weakness for pointers (as Joshy so elegantly pointed out)
I have no idea how to convert this statement:

Code: Select all

    var connection = client.GetConnection()
so that it points to pClient.

Once I have got that, then I an do the same with connection.
Regards
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Compile Errors from "var shared ...."

Post by fxm »

fxm wrote:...(then 'pclient->x' to access the field 'x')
var connection = pclient->GetConnection()
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Compile Errors from "var shared ...."

Post by Dinosaur »

Hi All

Many thanks for your valuable time on that.
That compiles and I can now move forward.

Regards
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Compile Errors from "var shared ...."

Post by fxm »

Happy that you progress.

Perhaps:
- In the main code:
Dim Shared As NetworkClient Ptr pclient
Dim Shared As NetworkConnection Ptr pconnection

- In the Sub:
pclient = New NetworkClient(ServerIP,ServerPort)
pconnection = pclient->GetConnection()
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Compile Errors from "var shared ...."

Post by Dinosaur »

Hi All

fxm, just an update.
The following sub works great, reporting 1.8 micro seconds Loop time.
EDIT: 0.18 micro Secs
The relevant Declares:

Code: Select all

Dim Shared As NetworkClient Ptr pclient(1 to 20)
Dim Shared As NetworkConnection Ptr pconnection(1 to 20)

Code: Select all

Sub DataTxRx
    Select case StepNbr(Device)
        Case 0
            If pClient(Device) < 1 Then
                pclient(Device) = New NetworkClient(ServerIP(Device),ServerPort)
                pconnection(Device) = pclient(Device)->GetConnection()
            EndIf
            StepNbr(Device) += 1
        Case 1
            pclient(Device)->GetConnection()
            StepNbr(Device) += 1
        Case 2
            If pconnection(Device)->CanPut() = 1 Then
                pConnection(Device)->PutData(StrPtr(TxData),Len(TxData))
                StepNbr(Device) += 1
            EndIf
        Case 3
            If pConnection(Device)->CanGet() = 1 Then
                var nBytes   = pConnection(Device)->GetData(buffer)
                var LastChar =instr(*buffer,HeaderEnd)-1
                var Header   =left(*buffer,LastChar)
                var DataStart=LastChar+4
                dim as ubyte ptr FileBuffer=@buffer[DataStart]
                For Cnt = 1 to nBytes
                    RxData += Chr(*(FileBuffer + Cnt))
                Next
                StepNbr (Device)+= 1
            EndIf
        Case 4
            decrypt(RxData)
            Do
                Cnt = Instr(RxData,",") 
                If Cnt Then
                        Mid(RxData,Cnt) = Chr(&H0A)
                    Else
                        Exit Do
                EndIf
            Loop
            StepNbr(Device) += 1
        Case 5
            If Instr(RxData, "err_code") > 0 Then
                If Instr(RxData,":0}}") > 0 Then
                        If PrintFlag > 0 Then
                            Print RxData
                            Print "No Error"
                        EndIF
                    Else
                        Print RxData
                EndIf
            EndIf
            StepNbr(Device) += 1
    End Select
End Sub
And the calling Code.

Code: Select all

    With Times
        Device = 1: ServerIP(Device) = "192.168.1.145"
        .StartTime  = (Timer - .CalTime) * 1000
        Do
            .uSec = (Timer - .CalTime) * 1000000
            DataTxRx
            .LoopTime = ((Timer - .CalTime) * 1000000) - .uSec
            If .LoopTime > .MaxTime Then .MaxTime = .LoopTime
            If StepNbr(Device) = 6 Then exit Do
        Loop
        Print "Loop Time  =";.MaxTime;" micro Secs"
        Print "Total Time =";((Timer - .CalTime) * 1000) - .StartTime;" milli Secs"
        If ErrFlag < 1 Then
                DataTxRx
                .mSec = (Timer - .CalTime) * 1000
                Print "Elapsed Time ="; .mSec - .StartTime
        EndIf
    End With
Once again many thanks.
Regards
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Compile Errors from "var shared ...."

Post by fxm »

Remark about the above topic

Although "simple" global (shared) variables such as integers or doubles currently only allow constant initializers, FreeBASIC supports non-constant initializers in the case where the global (shared) variable is a type defined by the user with constructor: the constructor of the global variable must be called anyway when the program starts, so it can also allow non-constant initializers that will end up being arguments in the constructor call.

This works for any type of constructor (even if no default constructor is defined), and even if the constructor called by the initializer of the declaration is in a library built from a FreeBASIC module:

Code: Select all

' udt.bas to be compiled as library

Type UDT
  Dim As Integer I
  Declare Constructor (Byval i0 As Integer)
End Type

Constructor UDT (Byval i0 As Integer)
  This.I = i0
End Constructor

Code: Select all

#inclib "udt"

Type UDT
  Dim As Integer I
  Declare Constructor (Byval i0 As Integer)
End Type

Dim Shared As UDT u = UDT(123)  '' or 'Var Shared u = UDT(123)'

Print u.I

Sleep
Otherwise and that it does not work, another solution (as topic above) is to use a typed pointer declared global (without initializer), then in the code to dynamically create the object thus referenced by this global (shared) pointer:

Code: Select all

#inclib "udt"

Type UDT
  Dim As Integer I
  Declare Constructor (Byval i0 As Integer)
End Type

Dim Shared As UDT Ptr pu  '' 'Var' can not be used because it requires an initializer

pu = New UDT(123)
Print pu->I

Delete pu

Sleep
In any case (with the default constructor missing), if the initialization of the object (with a constructor having at least one parameter) must be executed in a procedure, only the latter solution can be used:

Code: Select all

#inclib "udt"

Type UDT
  Dim As Integer I
  Declare Constructor (Byval i0 As Integer)
End Type

Dim Shared As UDT Ptr pu  '' 'Var' can not be used because it requires an initializer

Sub init ()
  pu = New UDT(123)
End Sub

init()
Print pu->I

Delete pu

Sleep
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Compile Errors from "var shared ...."

Post by badidea »

I ran into the same issue with the code below. Normally I don't use var, but I tried to be lazy today.
Both backends (gas & gcc) produce weird error codes that are of no use finding the problem in code.
I consider it a bug in the compiler, but I don't see the bug on the sourceforge bug list.

Code: Select all

type log_entry_type
	dim as string time_, text
	declare operator cast () as string
end type

operator log_entry_type.cast () as string
   return time_ & " " & text
end operator

type logger_type
	dim as string fileName
	dim as integer numEntries
	dim as log_entry_type entry(any)
	declare constructor(fileName as string, bufferSize as integer)
	declare destructor
	declare function add(text as string) as integer
end type

constructor logger_type(fileName as string, bufferSize as integer)
	this.fileName = fileName
	numEntries = 0
	redim entry(bufferSize - 1)
end constructor

destructor logger_type
	erase entry
	fileName = ""
end destructor

function logger_type.add(text as string) as integer
	'add to buffer (at top)
	if numEntries <= ubound(entry) then numEntries += 1
	for i as integer = (numEntries - 1) to 1 step -1
		entry(i) = entry(i - 1)
	next
	entry(0).time_ = time
	entry(0).text = text
	'write to file
	if fileName = "" then
		return -1
	else
		dim as integer fileNum
		fileNum = freefile
		if open(fileName, for append, as fileNum) = 0 then 
			print #fileNum, entry(0)
			close fileNum
		else
			return -2
		end if
	end if
	return 0
end function

'test code

var shared logger = logger_type("", 5)

print logger.numEntries
logger.add("bla1")
logger.add("bla2")
logger.add("bla3")
print logger.numEntries

for i as integer = 0 to logger.numEntries - 1
	print logger.entry(i)
next
gas output:

Code: Select all

fbc32 -exx -w all "logger_test.bas" (in directory: /media/badidea/data/Projects/FreeBASIC/Miner)
logger_test.asm: Assembler messages:
logger_test.asm:994: Error: invalid character (0x18) in operand 1
logger_test.asm:995: Error: invalid character (0x18) in operand 1
logger_test.asm:996: Error: invalid character (0x18) in operand 1
logger_test.asm:1002: Error: invalid character (0x18) in operand 2
logger_test.asm:1006: Error: invalid character (0x18) in operand 2
Compilation failed.
gcc output:

Code: Select all

fbc64 -w all "logger_test.bas" (in directory: /media/badidea/data/Projects/FreeBASIC/Miner)
logger_test.c: In function ‘_GLOBAL__I’:
logger_test.c:292:21: error: ‘p’ undeclared (first use in this function)
  __builtin_memset( &p<P, 0, 24ll );
                     ^
logger_test.c:292:21: note: each undeclared identifier is reported only once for each function it appears in
logger_test.c:292:24: error: stray ‘\1’ in program
  __builtin_memset( &p<P, 0, 24ll );
                        ^
logger_test.c:292:23: error: ‘P’ undeclared (first use in this function)
  __builtin_memset( &p<P, 0, 24ll );
                       ^
logger_test.c:293:44: error: stray ‘\1’ in program
  FBSTRING* vr$2 = fb_StrAssign( (void*)&p<P, -1ll, (void*)"", 1ll, 0 );
                                            ^
logger_test.c:294:48: error: stray ‘\1’ in program
  _ZN11LOGGER_TYPEC1ER8FBSTRINGl( &LOGGER$, &p<P, 5ll );
                                                ^
Compilation failed.
SARG
Posts: 1766
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Compile Errors from "var shared ...."

Post by SARG »

I got no error message with gas, and with gcc only warnings.

Code: Select all

badidea.c: In function '_GLOBAL__I':
badidea.c:429:52: warning: passing argument 2 of '_ZN11LOGGER_TYPEC1ER8FBSTRINGu7INTEGER' from incompatible pointer type [-Wincompatible-pointer-types]
  _ZN11LOGGER_TYPEC1ER8FBSTRINGu7INTEGER( &LOGGER$, &_ZN11LOGGER_TYPEaSERKS_, 5ll );
                                                    ^
badidea.c:117:6: note: expected 'FBSTRING * {aka struct <anonymous> *}' but argument is of type 'void (*)(struct $11LOGGER_TYPE *, struct $11LOGGER_TYPE *)'
 void _ZN11LOGGER_TYPEC1ER8FBSTRINGu7INTEGER( struct $11LOGGER_TYPE* THIS$1, FBSTRING* FILENAME$1, int64 BUFFERSIZE$1 )
      ^
What version of fbc are you using ?
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Compile Errors from "var shared ...."

Post by D.J.Peters »

Windows fbc 32-bit GAS is OK

Windows fbc 32-bit GCC I get
FreeBASIC Compiler - Version 1.08.0 (2019-10-27), built for win32 (32bit) wrote:D:\FBIDE\FBIDETEMP.c: In function '_GLOBAL__I':
D:\FBIDE\FBIDETEMP.c:300:21: error: 'TMP$13$0' undeclared (first use in this function)
__builtin_memset( &TMP$13$0, 0, 12 );
^
D:\FBIDE\FBIDETEMP.c:300:21: note: each undeclared identifier is reported only once for each function it appears in
Windows fbc 64-bit I get
FreeBASIC Compiler - Version 1.08.0 (2019-10-27), built for win64 (64bit) wrote:D:\FBIDE\FBIDETEMP.c: In function '_GLOBAL__I':
D:\FBIDE\FBIDETEMP.c:296:21: error: 'TMP$13$0' undeclared (first use in this function)
__builtin_memset( &TMP$13$0, 0, 24ll );
^
D:\FBIDE\FBIDETEMP.c:296:21: note: each undeclared identifier is reported only once for each function it appears in
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Compile Errors from "var shared ...."

Post by paul doe »

I already noticed this issue, too. Why is var even allowed to declare shared variables is beyond me, though.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Compile Errors from "var shared ...."

Post by fxm »

fxm wrote:Remark about the above topic

Although "simple" global (shared) variables such as integers or doubles currently only allow constant initializers, FreeBASIC supports non-constant initializers in the case where the global (shared) variable is a type defined by the user with constructor: the constructor of the global variable must be called anyway when the program starts, so it can also allow non-constant initializers that will end up being arguments in the constructor call.
As that works with 'dim shared as logger_type logger = logger_type("", 5)', I consider that this is a bug from 'var shared'.
The bug seems around 'redim' in the constructor when called from 'var shared' to evaluate the expression of the initializer (otherwise works with a fix-len array member).
Post Reply