(SOLVED) Byref it seems incorrect description

Forum for discussion about the documentation project.
VANYA
Posts: 1370
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

(SOLVED) Byref it seems incorrect description

Postby VANYA » Mar 18, 2020 8:42

quote from: https://freebasic.net/wiki/KeyPgByrefVariables

Declares a reference

Syntax:
(Dim | Static) [Shared] Byref name1 As DataType = variable1 [, Byref name2 As DataType = variable2, ...]


But this is impossible ?!

example:

Code: Select all

type T
   
   dValue as double
   
End Type

dim p as T ptr = new T

dim shared byref sp as T = *p


SHARED in this case blocks the installation symbolic link
Last edited by VANYA on Mar 18, 2020 12:39, edited 1 time in total.
fxm
Posts: 9624
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Byref it seems incorrect description

Postby fxm » Mar 18, 2020 10:15

This problem is not exclusively due to the declaration of a reference, but more generally due to the initializer of the declaration 'Dim Shared' which must be a constant (or expression which can be evaluated at compile time, or global variable which can be evaluated at compilation).
See the SHARED documentation page:
    dim shared s as T
    works, but:
    dim shared s as T = *p
    does not work (constant expected).
For an 'UDT' instance to be able to serve as an initializer for 'Dim Shared', it must be global (therefore static declaration by consequence) and it must can to be evaluated at compile time (therefore a constructor must be defined explicitly).
Example:

Code: Select all

type T
   
   dValue as double
   declare constructor ()
   
End Type

constructor T
end constructor

dim shared t1 as T

dim shared t2 as T = t1
dim shared byref t3 as T = t1
    Note:
      - 'dim shared byref t3 as T = t1' without constructor works because no new global instance is created, only a global reference to an existing global instance,
      - declare an explicit constructor can be replaced with a member data initializer (as 'dValue as double = 0')
But in this case, 'New' usage is disallowed because such an initializer instance is dynamic (so not static).

One can never define a global reference to a dynamic variable, but only a local reference.
VANYA
Posts: 1370
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Byref it seems incorrect description

Postby VANYA » Mar 18, 2020 11:20

It turns out this design:

Code: Select all

dim shared byref sp as T


It is virtually useless to the programmer as a symbolic link is needed for UDT types (dynamic), to simplify the code. But for simple variables (with static linking), they are like a dog the fifth leg.

In any case, you fxm thank you very much!
fxm
Posts: 9624
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Byref it seems incorrect description

Postby fxm » Mar 18, 2020 12:02

But for that, a two-line workaround (see Using References, paragraph 'Hacking on usage of references with the additional syntaxes allowed by FreeBASIC'):

Code: Select all

type T
    dValue as double
End Type

'' Instead of 'dim shared byref sp as T = *new T', a two-line workaround
    dim shared byref sp as T = *cptr(T ptr, 0)
    @sp = new T

sp.dValue = 4 * atn(1)
print sp.dValue

sleep
delete @sp
VANYA
Posts: 1370
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Byref it seems incorrect description

Postby VANYA » Mar 18, 2020 12:39

fxm wrote:Hacking on usage of references with the additional syntaxes allowed by FreeBASIC


This is interesting in terms of education, but is unreliable because in future versions may vary. fxm, my gratitude to you for your help and most importantly for your work in the design documentation.
fxm
Posts: 9624
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: (SOLVED) Byref it seems incorrect description

Postby fxm » Mar 19, 2020 8:39

I think that these FreeBasic additional syntaxes (compared to C for example):

Code: Select all

Dim Byref As datatype ref = *Cptr(datatype Ptr, 0)
'and
@ref = @other_variable
will be preserved in future revisions (and I agree with that), because as @dkl said similarly in a post in August 2016 (viewtopic.php?p=222765#p222765):
dkl wrote:...
And consider this:

Code: Select all

sub f(byref i as integer)
end sub

dim pi as integer ptr = 0
f(*pi)
You can pass a NULL reference, and it won't crash or be caught by -exx until accessed.

Or another, related case:

Code: Select all

type UDT
   as integer a, b, c
end type

print @cptr(UDT ptr, 0)->c
That's how offsetof() is implemented, should -exx catch that too? There's a NULL pointer dereference in the source code, but not in the final program. Maybe that's the main inconsistency issue here?
fxm
Posts: 9624
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: (SOLVED) Byref it seems incorrect description

Postby fxm » Mar 20, 2020 6:42

As it should not be confused:

Code: Select all

print @cptr(UDT ptr, 0)->c
'with:
print cptr(UDT ptr, 0)->c
similarly, it should not be confused:

Code: Select all

Dim Byref As datatype ref = *Cptr(datatype Ptr, 0)
'with:
Dim As datatype ref = *Cptr(datatype Ptr, 0)

The first two expressions correspond to an internal pointer value assignment while the second two expressions correspond to a real dereferencing.
VANYA
Posts: 1370
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: (SOLVED) Byref it seems incorrect description

Postby VANYA » Mar 20, 2020 11:02

fxm buddy!

I was interested in this functionality for a very practical purpose. I have VECTOR CODE, it is written and it is undesirable to edit it, since the sources codes using it should just include it without any changes.

And for example, there is CODE that uses the VECTOR code. You can pay attention that due to the fact that it is impossible to create a global link, I had to pass the link in the parameters of the function in all functions.

Return to “Documentation”

Who is online

Users browsing this forum: No registered users and 2 guests