QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Pete on October 07, 2021, 05:39:26 pm

Title: I discovered this difference between QB and QB64... [My mistake. No Difference.]
Post by: Pete on October 07, 2021, 05:39:26 pm
QB64 allows this to run...

Code: QB64: [Select]
  1. DIM a$(100)
  2. a$(11) = "x"
  3. PRINT a$(11)
  4. PRINT a$(11)

QB does not, throwing a subscript out of range error at line 5.

Note that both QB64 and QB throw that error if REDIM is used instead of DIM...

Code: QB64: [Select]
  1. ]REDIM a$(100)
  2. a$(11) = "x"
  3. PRINT a$(11)
  4. PRINT a$(11)

Pete

Edit: It turned out I made some sort of error. I retyped the code in QuickBASIC, and the DIM example worked the same as QB64.
Title: Re: I discovered this difference between QB and QB64
Post by: SMcNeill on October 07, 2021, 05:44:51 pm
If you use REDIM, then just:

REDIM a$(100)
a$(11) = "x"
PRINT a$(11)
REDIM a$(100)
PRINT a$(11)

Without _PRESERVE, REDIM will erase your contents for you as well.
Title: Re: I discovered this difference between QB and QB64
Post by: Pete on October 07, 2021, 05:54:29 pm
Oh, not needing a way around it. QB64 is supposed to react like QB and this is one of the few instances where it acts differently. It is not a critical issue, because in this case, QB64 allows something that QBasic did not. If it were the other way around, it would need to be fixed or at least addressed in the wiki.

So QB64 allows us to do this...

Code: QB64: [Select]
  1. DIM a$(100)
  2. a$(10) = "erase": a$(11) = "x"
  3. PRINT a$(10), a$(11)
  4. a$(11) = "y"
  5. PRINT a$(10), a$(11)

Can't run that to completion in QuickBASIC.

Pete
Title: Re: I discovered this difference between QB and QB64
Post by: SMcNeill on October 07, 2021, 06:31:06 pm
QuickBASIC was better at finding ways to clean up after itself than QB64 is.

DIM a(1000)
ERASE a(1000)

The above freed all the memory which a() used, letting us reuse it for something else in Qbasic.  In QB64, your array is still there; we just blank the data in it back to nothing...

The real question becomes: If you ever actually wanted/needed to free the memory that a() uses in QB64, how the heck would you do that?  Can we do that?  Seems to me that cleaning up memory isn’t anywhere near as important as it used to be, and it's something QB64 isn’t half as good at as QB45 used to be.
Title: Re: I discovered this difference between QB and QB64
Post by: Pete on October 07, 2021, 06:53:45 pm
That's the same conclusion I came to as well. In QB64, memory is no longer an issue. Now if I can remember this, I can use ERASE in QB64 to preserve the DIMMED array bounds, without needing to to a REDIM. I may never get around to that, as I usually exclusively use REDIM for all my arrays.

Pete
Title: Re: I discovered this difference between QB and QB64
Post by: George McGinn on October 07, 2021, 10:01:14 pm
Don't you need $DYNAMIC to use REDIM?
Title: Re: I discovered this difference between QB and QB64
Post by: Pete on October 07, 2021, 10:09:05 pm
I'm going to say no, as I have not used '$DYNAMIC in any of my routines that I use REDIM. I do recall using the DYNAMIC metacommand in the QBasic days, as it saved memory.

Maybe others will have some more technical info...

Pete
Title: Re: I discovered this difference between QB and QB64
Post by: bplus on October 07, 2021, 10:12:22 pm
Don't you need $DYNAMIC to use REDIM?

No, I use ReDim to start a dynamic array. You could use ReDim for everything! (except for STATIC in Subroutine)
Title: Re: I discovered this difference between QB and QB64
Post by: SMcNeill on October 07, 2021, 11:50:01 pm
Don't you need $DYNAMIC to use REDIM?

$DYNAMIC defaults everything to REDIM status; without it, you have to declare your array redimmable from the start.


EXAMPLE:

DIM X(10)
REDIM X(100)

The above will error out.  X isn't initially declared redimable, so you can't resize it.


EXAMPLE 2:

$DYNAMIC
DIM X(10)
REDIM X(100)

The above here works.  $DYNAMIC defaults all arrays to be redimmable, so REDIM works with no issues.



EXAMPLE 3:
DIM X(10)
REDIM Y(10)

REDIM X(100)
REDIM Y(100)


Now here, we initialize X(10) with DIM, making it non-resizable.  Y(10), however, we initialize with REDIM, making it resizable.

The REDIM X(100) will toss an error as we can't resize the array X().
On the other hand, there's no issue with resizing Y() to 100 elements.



If you DIM an array, it's non-resizable, UNLESS $DYNAMIC makes everything resizable.
If you use REDIM from the start, then your array begins resizable and remains that way forever more.
Title: Re: I discovered this difference between QB and QB64
Post by: bplus on October 08, 2021, 12:46:52 am
To clear up a matter, if your originally use ReDim to start an array it is Dynamic.

If you originally start an array with DIM it is Static unless you use $Dynamic before hand.

Here is a dynamic demo without need of $Dynamic:
Code: QB64: [Select]
  1. ReDim a$(5) ' here is a dynamic array because I used redim and not dim to setup
  2.  
  3. For i = 0 To 5
  4.     a$(i) = Str$(i)
  5.     Print a$(i);
  6. Print ", so now a$ has 6 items."
  7.  
  8. fini = Int(Rnd * 20) + 1 ' at least 1 element
  9. Print "going to add"; fini; " more items."
  10.  
  11. For i = 6 To 6 + fini
  12.     sAppend a$(), Str$(i)
  13. Print "lbound(a$); ubound(a$) = "; LBound(a$); UBound(a$)
  14. Print "Contents of a$:"
  15. For i = LBound(a$) To UBound(a$)
  16.     Print a$(i);
  17.  
  18.  
  19. 'here is a handy array appending tool I have used many a time
  20. ''append to the string array the string item
  21.  
  22. ' arr() does have to be either started with ReDim to use here or $Dynamic listed before hand.
  23. Sub sAppend (arr() As String, addItem$) ' note the first element will be left blank
  24.     ReDim _Preserve arr(LBound(arr) To UBound(arr) + 1) As String
  25.     arr(UBound(arr)) = addItem$
  26.  
  27.  
  28.  
Title: Re: I discovered this difference between QB and QB64
Post by: bplus on October 08, 2021, 12:57:11 am
Sorry Steve we are the redundant brother again, but I just got PM and decided to make public my demo.
Title: Re: I discovered this difference between QB and QB64
Post by: SMcNeill on October 08, 2021, 01:20:28 am
Sorry Steve we are the redundant brother again, but I just got PM and decided to make public my demo.

Brilliant minds think alike -- so why are you copying ME?!!  😂😂
Title: Re: I discovered this difference between QB and QB64
Post by: FellippeHeitor on October 09, 2021, 10:29:41 am
QB64 allows this to run...

Code: QB64: [Select]
  1. DIM a$(100)
  2. a$(11) = "x"
  3. PRINT a$(11)
  4. PRINT a$(11)

QB does not, throwing a subscript out of range error at line 5.

Note that both QB64 and QB throw that error if REDIM is used instead of DIM...

Code: QB64: [Select]
  1. ]REDIM a$(100)
  2. a$(11) = "x"
  3. PRINT a$(11)
  4. PRINT a$(11)

Pete

I just tried the first snippet on QuickBASIC 4.5 and got not error - so the behaviour is identical to what we got in QB64. Are you maybe talking about QBasic instead?
Title: Re: I discovered this difference between QB and QB64
Post by: FellippeHeitor on October 09, 2021, 10:32:20 am
Nah, I just tried in QBasic. No error either. Care to provide more detail?
Title: Re: I discovered this difference between QB and QB64
Post by: FellippeHeitor on October 09, 2021, 10:34:48 am
Oh, not needing a way around it. QB64 is supposed to react like QB and this is one of the few instances where it acts differently. It is not a critical issue, because in this case, QB64 allows something that QBasic did not. If it were the other way around, it would need to be fixed or at least addressed in the wiki.

So QB64 allows us to do this...

Code: QB64: [Select]
  1. DIM a$(100)
  2. a$(10) = "erase": a$(11) = "x"
  3. PRINT a$(10), a$(11)
  4. a$(11) = "y"
  5. PRINT a$(10), a$(11)

Can't run that to completion in QuickBASIC.

Pete

This one ran fine in both QuickBASIC 4.5 and in QBasic 1.1.
Title: Re: I discovered this difference between QB and QB64
Post by: Cobalt on October 09, 2021, 11:44:51 am
I'm with Fellippe, the example you stated to error out seems to work fine with no errors in QB45.


Title: Re: I discovered this difference between QB and QB64
Post by: bplus on October 09, 2021, 11:49:28 am
Maybe Pete's Peeked and Poked one too many times?
Title: Re: I discovered this difference between QB and QB64
Post by: Cobalt on October 09, 2021, 12:27:17 pm
Maybe Pete's Peeked and Poked one too many times?

you mean "and been Poked" Darn aliens!

did QB45 come with California's "Prop 65" warning? Maybe he can sue!(cause everything in Cali' causes cancer, even the air)
Title: Re: I discovered this difference between QB and QB64
Post by: MWheatley on October 09, 2021, 05:02:49 pm
Runs here, too, in QB45 and PDS 7.1, although I haven't tried in QBasic, as that's not presently installed on this machine.

Malcolm
Title: Re: I discovered this difference between QB and QB64
Post by: Pete on October 11, 2021, 10:27:08 pm
@FellippeHeitor  Well I got back, read the posts, and dug up the old Win 7. I didn't save the code from before, so I typed it back in QuickBasic, and ran it in DOSBox. So this time, the program with DIM ran to completion. So whatever happened before to make it  throw a subscript out of range error did not happen this time, and as we all know with computers, that means whatever I did last time was not the same. So my apologies. I don't know if this was some senior moment or eyesight situation, because my DOSBox renders a very small IDE. Now I wished I saved the damn snippet, so I could see what happened. My best guess is I may have typed a different variable like a1$(11) instead of a$(11). Anyway, a "blonder" moment indeed.

Conclusion. QB and QB64 act exactly the same way in the situations described.

Pete   
Title: Re: I discovered this difference between QB and QB64
Post by: FellippeHeitor on October 11, 2021, 10:28:29 pm
Glad it's cleared now. Thanks for confirming.
Title: Re: I discovered this difference between QB and QB64... [My mistake. No Difference.]
Post by: Cobalt on October 12, 2021, 04:00:44 am
No excuse!

You are here by sentenced to....

20 years hard coding!

“Start codein’ ya doggone long eared galoot”

Title: Re: I discovered this difference between QB and QB64... [My mistake. No Difference.]
Post by: Pete on October 12, 2021, 10:21:13 am
You mean like this?

Code: QB64: [Select]
  1. PRINT "Eat me, varmint!"
  2. LOOP UNTIL HellFreezesOver

Look on the bright side, at least you got me to try CamelCase.

Pete