Author Topic: $CHECKING:OFF is broken with arrays (at least sometimes)  (Read 4476 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
$CHECKING:OFF is broken with arrays (at least sometimes)
« on: June 20, 2020, 05:57:12 am »
Not certain how widespread the issue here is, but here's a little sample code to highlight the issue:

Code: QB64: [Select]
  1. DIM wins(-2 TO 0)
  2.  
  3. wins(-2) = 1
  4.  
  5. FOR i = -2 TO 0
  6.     PRINT wins(i)
  7.  

I can't think of any way to highlight the issue any better than the above.  We set wins(-2) to a value of 1, print the values, and see it's....  still 0!

Maybe Rho can take a moment and dig into this for us, and see if it's something which his alterations to $CHECKING and arrays didn't take into account, with negative indexes.  If it's not something in those changes which is causing the issue, I have no idea what else it might be at the moment, but it's obviously glitching out on us.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: $CHECKING:OFF is broken with arrays (at least sometimes)
« Reply #1 on: June 20, 2020, 06:22:05 am »
Hold on, checking...
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: $CHECKING:OFF is broken with arrays (at least sometimes)
« Reply #2 on: June 20, 2020, 07:27:53 am »
I see wut u did there...
You're not done when it works, you're done when it's right.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: $CHECKING:OFF is broken with arrays (at least sometimes)
« Reply #3 on: June 20, 2020, 08:41:35 am »
So there's indeed a side effect on negative indicies of the array_check() routine in qbx.cpp, which is switched on/off by using CHECKING:ON/OFF:
Code: C: [Select]
  1. inline ptrszint array_check(uptrszint index,uptrszint limit){
  2.     //nb. forces signed index into an unsigned variable for quicker comparison
  3.     if (index<limit) return index;
  4.     error(9); return 0;
  5. }
  6.  
  7.  

but the problem here arises as you're not consistent in your programming, you assign values in CHECKING:OFF mode, so array_check() can't twist any indicies, but you then recall the values (PRINT) in CHECKING:ON mode, so array_check() will twist the negative indicies, hence you essencially recall values from different indicies than those which you assigned the values too, simply comment out the CHECKING:ON, so the PRINT can run in CHECKING:OFF mode too, and it will work:
Code: QB64: [Select]
  1. DIM wins(-2 TO 1)
  2.  
  3. wins(-2) = -2
  4. wins(-1) = -1
  5. wins(0) = 0
  6. wins(1) = 1
  7. '$CHECKING:ON
  8.  
  9. FOR i = -2 TO 1
  10.     PRINT wins(i)
  11.  
  12.  

The question now is, how to tackle this issue the best way. As it is obviously (in a bigger program) hard to keep track, that every array accesses are done in the same mode (CHECKING:ON/OFF) and this is probably also not always desireable, it should be done to be independend of the use of the array_check() routine, which is switched on/off by CHECKING.

1.) Waive to negative indicies (IMHO something weird anyways)
2.) Waive to the use of CHECKING:OFF completely when using arrays
3.) Revoke my changes regarding CHECKING:OFF in arrays (seems the easiest, it's only deleting a coulple lines)
4.) Find another way, which can deal with negative indicies as well as random CHECKING modes

I know what I'm doing with my personal copy of QB64, --- I'll do nothing ---, as I'd never ever in my weirdest dreams get the idea to use negative array indicies at all, hence the problem doesn't exist for me. Negative array indicies are as unusual and useless to me like OBPTION BASE 1 and OPTION _EXPLICIT, but as it is a matter for the entire QB64 user base, I'll listen to your thoughts, however revoking my changes seems to be the easiest and most safe way, as every experimenting could introduce new unexpected array behavior.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: $CHECKING:OFF is broken with arrays (at least sometimes)
« Reply #4 on: June 20, 2020, 10:47:33 am »
I've pushed a tentative fix for this.

The problem is that the $checking:off path wasn't generating the subtraction to adjust for a custom array lbound.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: $CHECKING:OFF is broken with arrays (at least sometimes)
« Reply #5 on: June 20, 2020, 12:07:42 pm »
Ah, I see now what I've missed, but its rather adding in again half of the stuff I was hoping to get rid of in CHECKING:OFF mode, but as this is obviously not possible without causing problems, I'd plead to remove it completly instead. However, some could say saving half of the overhead is still better than save nothing in CHECKING:OFF.

EDIT:
Another question is, if it is really necessary to overcomplicate those things, just for the probably very rarely used feature to use negative array indicies. Steve's example is in fact the first use I've ever seen here.

And by the way the Wiki page for DIM warns about negative UBOUNDs anyways, why not extending this warning to the LBOUNDs either and rather condemn the usage of negative indicies at all?
« Last Edit: June 20, 2020, 12:20:42 pm by RhoSigma »
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: $CHECKING:OFF is broken with arrays (at least sometimes)
« Reply #6 on: June 20, 2020, 12:28:09 pm »
It's got nothing to do with negative indices in particular, it affects any array with an lbound != 0.


Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: $CHECKING:OFF is broken with arrays (at least sometimes)
« Reply #7 on: June 20, 2020, 12:45:07 pm »
It's got nothing to do with negative indices in particular, it affects any array with an lbound != 0.

See that's the reason why I never had problems with it since I've implemented the CHECKING:OFF modifications mid/late 2018, as zero is the one and only LBOUND existing in my world :)

There's no clue for me why implementing test(13 TO 19) not simply as test(0 to 6), or even simplier as test(6), as I don't have OPTION BASE 1 either in my world :)

Ah, I know it's a simple nice and easy world I live in...
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: $CHECKING:OFF is broken with arrays (at least sometimes)
« Reply #8 on: June 20, 2020, 03:12:12 pm »
Quote
Ah, I know it's a simple nice and easy world I live in...

Based on nothing ;-))