Self-referential initialisation by Dim.

General FreeBASIC programming questions.
Richard
Posts: 2938
Joined: Jan 15, 2007 20:44
Location: Australia

Self-referential initialisation by Dim.

Postby Richard » Mar 15, 2019 2:20

There was a time when Constants could be declared that referred to themselves.
That time has now passed and the compiler correctly complains.

But now I find I can Dim and initialise a self-referential variable.
Is this a new bug, or is it necessary or acceptable for some reason ?

Code: Select all

Dim As Double a = a
Print a
' which again makes possible things like;
Dim As Double Pi = Atan2( Pi<>Pi, Pi=Pi )
Print Pi
print __FB_version__    ' my version is 1.04.0
fxm
Posts: 8972
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Self-referential initialisation by Dim.

Postby fxm » Mar 15, 2019 6:43

We have already played this, but I have not found the thread?

Code: Select all

Dim As Double a
Print "a = "; a

Dim As Double b = b
Print "b = "; b

Dim As Double c = c = c
Print "c = "; c

Dim As Double d = d = d = d
Print "d = "; d

Sleep
Tourist Trap
Posts: 2756
Joined: Jun 02, 2015 16:24

Re: Self-referential initialisation by Dim.

Postby Tourist Trap » Mar 15, 2019 13:38

Richard wrote:(dim Pi =)
Pi<>Pi
Pi=Pi

For me, I see no problem. Even if the symbol has not been defined, the results here, at least for these operations are predictible (FALSE, TRUE, or 0, 1). Even more, those things have a well defined result if precisely Pi is not a function, a macro, anything previously defined. And if Pi were to be defined previously in one of those ways, I think that the compiler wouldn't allow the DIM instruction. So I see no problem at all. At least from my point of view (I see this like a computation on a blank square symbol, no variable name is needed to compute the whole stuff).

For the most curious case, it seems that "dim as integer x = x" means "dim as integer x = any" , yes?
jj2007
Posts: 1161
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Self-referential initialisation by Dim.

Postby jj2007 » Mar 15, 2019 14:45

Tourist Trap wrote:For the most curious case, it seems that "dim as integer x = x" means "dim as integer x = any" , yes?

Code: Select all

  dim as integer a=a
  print "a=";a
Gas says a= 4218880, which happens to be the entry point. Gcc loads [ebp-40], which may be 0 or -1. Not everything that looks like code is useful code ;-)
Tourist Trap
Posts: 2756
Joined: Jun 02, 2015 16:24

Re: Self-referential initialisation by Dim.

Postby Tourist Trap » Mar 15, 2019 14:51

jj2007 wrote:

Code: Select all

  dim as integer a=a
  print "a=";a
Gas says a= 4218880, which happens to be the entry point. Gcc loads [ebp-40], which may be 0 or -1.


I don't really know if I run GCC or GAS, but it looks rather random as the any initialization.

Code: Select all

dim as integer x = x
? x
dim as integer y = y
? y
dim as integer z = z
? z

4211001
1
4210037


jj2007 wrote: Not everything that looks like code is useful code ;-)

It's rather useless code, that's why I love this. It's useless but doesn't seems to contain anything wrong mathematically :)
Richard
Posts: 2938
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Self-referential initialisation by Dim.

Postby Richard » Mar 15, 2019 14:55

Tourist Trap wrote:For the most curious case, it seems that "dim as integer x = x" means "dim as integer x = any" , yes?

Dim As Integer x = Any; does not initialise x so it saves time.
Dim As Integer x = x; has the same outcome but wastes time doing it, so I see no advantage in the feature.

Where the Dim statement supports a long initialisation expression, there is no warning if you incorrectly use the symbol you have not yet defined, within that expression. That is how one of my many typos conjured up the unexpected behaviour.
Tourist Trap
Posts: 2756
Joined: Jun 02, 2015 16:24

Re: Self-referential initialisation by Dim.

Postby Tourist Trap » Mar 15, 2019 15:14

Richard wrote:Dim As Integer x = x; has the same outcome but wastes time doing it, so I see no advantage in the feature.

Hi Richard,
but how is this expression parsed? I can't see how it can be verified from the left right sorry, since "x" is not yet defined in isolation there. Maybe then, but it's only a conjecture, the whole expression is recognized as an equivalent of "Dim As Integer x = Any"?
counting_pine
Site Admin
Posts: 6169
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Self-referential initialisation by Dim.

Postby counting_pine » Mar 15, 2019 15:24

It's a long-standing bug in the compiler. I'm not sure a report was filed, but a thread about it is here:
viewtopic.php?f=3&t=19752

EDIT: other threads:
viewtopic.php?p=88956#p88956
viewtopic.php?p=93504#p93504

It's a potentially dangerous bug, because it allows objects to be initialised from uninitialised copies from themselves, i.e. an indefined area of memory.

Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests