Author Topic: I discovered this difference between QB and QB64... [My mistake. No Difference.]  (Read 6285 times)

0 Members and 1 Guest are viewing this topic.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
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.
« Last Edit: October 11, 2021, 10:30:08 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: I discovered this difference between QB and QB64
« Reply #1 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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: I discovered this difference between QB and QB64
« Reply #2 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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: I discovered this difference between QB and QB64
« Reply #3 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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: I discovered this difference between QB and QB64
« Reply #4 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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: I discovered this difference between QB and QB64
« Reply #5 on: October 07, 2021, 10:01:14 pm »
Don't you need $DYNAMIC to use REDIM?
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: I discovered this difference between QB and QB64
« Reply #6 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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: I discovered this difference between QB and QB64
« Reply #7 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)

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: I discovered this difference between QB and QB64
« Reply #8 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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: I discovered this difference between QB and QB64
« Reply #9 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.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: I discovered this difference between QB and QB64
« Reply #10 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.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: I discovered this difference between QB and QB64
« Reply #11 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?!!  😂😂
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

FellippeHeitor

  • Guest
Re: I discovered this difference between QB and QB64
« Reply #12 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?

FellippeHeitor

  • Guest
Re: I discovered this difference between QB and QB64
« Reply #13 on: October 09, 2021, 10:32:20 am »
Nah, I just tried in QBasic. No error either. Care to provide more detail?

FellippeHeitor

  • Guest
Re: I discovered this difference between QB and QB64
« Reply #14 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.