QB64.org Forum
Active Forums => QB64 Discussion => Topic started by: bplus on October 07, 2021, 04:09:14 pm
-
Should Erase work for clearing values in a STATIC array? I tried it here
https://www.qb64.org/forum/index.php?topic=4266.msg136545#msg136545
Got errors, so I ended up doing it the hard way ie, resetting each and every value back to 0 in 2 index For loops for 2D arrays. I am wondering if I just screwed up with Erase.
-
Try ERASE
x$(11) = "Steve"
PRINT x$
(11);
" Hey, where'd Steve go?"
But I get it now... I know it works with DIM, but not REDIM. Also, I just looked at your post, tried this experiment, and it fails. Now I see your problem...
x$(11) = "Steve"
PRINT x$
(11);
" Hey, where'd Steve go?"
So now I'm wondering how ERASE worked in QuickBASIC?
Okay, for a QBasic page, I get this...
For static arrays, ERASE sets each element of a numeric array to zero and each element of a string array to null.
For dynamic arrays, ERASE frees the memory used by the array. You must redeclare the array's dimensions with REDIM or DIM before using it.
So maybe this is a QB64 bug. Sorry, I don't have QBasic on this machine.
Pete
-
Thanks Pete an alternate illustration of my Point.
-
Ah, in QBasic, we were not able to code: STATIC x$(1 to 100)
QB only accepted: STATIC x$()
So if I were coding this in QB, I'd have to do the following to zero my arrays and reuse them...
x$(11) = "Steve"
REDIM x$
(1 to 100) ' Sets all elements to null. PRINT x$
(11);
" Hey, where'd Steve go?"
So you could use ERASE this way, but you'd still have to do the REDIM. It turns out in QB ERASE does not preserve the array bounds. QB64 does the same, so not a bug. So I would just REDIM and forget about ERASE, because you intend to use the array again. Is this what you are looking for?
Pete
-
STATIC X(1000)
DIM M AS _MEM: M = _MEM(X())
For I = 0 to 1000
X(i) = i
NEXT
_MEMFILL M, M.OFFSET, M.SIZE, 0 AS _BYTE ‘reset the array
FOR I = 0 to 1000
Print X(I)
Next
-
@SMcNeill
Which is the same as...
Right?
or...
Right?
BTW - I posted about a related inconsistency between QB and QB64. Have a look, if you have the time.
Pete
-
It is. It's just a faster and easier way to do the same thing. ;)
-
STATIC X(1000)
DIM M AS _MEM: M = _MEM(X())
For I = 0 to 1000
X(i) = i
NEXT
_MEMFILL M, M.OFFSET, M.SIZE, 0 AS _BYTE ‘reset the array
FOR I = 0 to 1000
Print X(I)
Next
This assumes default Single Type, correct?
Or this might figure the Return according to X() Type
M = _MEM(X())
-
This assumes default Single Type, correct?
Or this might figure the Return according to X() Type
M = _MEM(X())
Any type but strings. _MEM won't work with variable length strings, and if you're using fixed length, you'd probably want to fill with 32 instead of 0. (Spaces CHR$(32) rather than NULL CHR$(0).)