Self-referential initialisation by Dim.

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

Self-referential initialisation by Dim.

Post by Richard »

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
Moderator
Posts: 12112
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Self-referential initialisation by Dim.

Post by fxm »

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: 2958
Joined: Jun 02, 2015 16:24

Re: Self-referential initialisation by Dim.

Post by Tourist Trap »

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: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Self-referential initialisation by Dim.

Post by jj2007 »

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: 2958
Joined: Jun 02, 2015 16:24

Re: Self-referential initialisation by Dim.

Post by Tourist Trap »

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: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Self-referential initialisation by Dim.

Post by Richard »

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: 2958
Joined: Jun 02, 2015 16:24

Re: Self-referential initialisation by Dim.

Post by Tourist Trap »

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: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Self-referential initialisation by Dim.

Post by counting_pine »

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

EDIT: other threads:
https://freebasic.net/forum/viewtopic.p ... 956#p88956
https://freebasic.net/forum/viewtopic.p ... 504#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.
Post Reply