LoginLogin

Arrays pointing at other things

Root / Programming Questions / [.]

MariominerCreated:
This is kind of odd, and was brought to light to me by IAmAPersson (I get worried that I spelled that wrong every single time, but I'm pretty certain this time). So I was working on ZOS when something odd happened. ZOS works by splitting a program up into individual labels with code in them, then using GOSUB to run each line. However, things took a twist when I tried to use an array. This is what it looked like.
'THE PROGRAM BEING RUN IN ZOS:
@FEXP_CODE_0
DIM F$[0]
RETURN
'ZOS
''POINTLESS LOOPS THAT ADD NOTHING TO THE MEANING OF THIS POST
GOSUB "1:@FEXP_CODE_"+STR$(I) 'I=0
But then what happened? Well, I ran the code and used the array F$ in FILES. FILES F$,"/" Which gave me a Type Mismatch error. Obviously the string literal is indeed a string literal, so then I decided to test what F$ was.
?F$+1
TYPE MISMATCH
?F$[0]
TYPE MISMATCH
?F$
TYPE MISMATCH
I concluded that F$ was nothing, but after IAmAPersson's suggestion that I may have turned it into a pointer, I tested that theory.
DIM A$[10]
A$[0]="HELLO"
A$[1]="WORLD"
F$=A$
?F$[0]
HELLO
So thus, I conclude that F$ must be a pointer. However, it doesn't just point at arrays, and it gets odd after getting used to the type suffixes.
VAR H=99
F$=H
F$=F$-1
?H
98
TL;DRI had a program that declared an array in a GOSUB in another slot, and it turned into a pointer afterwards, pointing to types that weren't even of the same suffix.
Now, I think I have a general idea of what is happening, so here it is. When the program GOSUBs to the label, it declares the array. The array is in slot 1, however it is being run by slot 0, so it is set as an array in slot 1, being assigned a pointer to use. Then, the interpreter returns to slot 0. When the program later returns to a later line with FILES, it does find an array in slot 1, but it's pointer is stored by the program in slot 0 (or possibly even forgotten). Thus, it is a NULL and Void pointer, and the function FILES called for an array. Then again, I'm probably wrong with at least one of those things.

Arrays act like pointers. You can assign a multi dimensional array into one dimensional array. Example
var a[0]
var b[2,2]
b[1,1]=55
a=b
? a[1,1] // show 55
Also, the memory that they use remain until the last reference is lost.
var a[1000000]
var b[0]
var c[0]
? FREEMEM  //327124
a=b
? FREEMEM //327124
b=c
? FREEMEM //327124
a=c
? FREEMEM //8327164
Finally, you can allocate an array using function.
DEF malloc%(size)
 VAR A%[size]
 return A%
END

VAR ARR%[0]
ARR%=malloc%(5)
The only thing we can't do is assign a numeric array with a string array. That will trigger the Type mismatch error.

I know that arrays are pointer, which is why I put pointing at "other things" in the title. However, it's odd that they can be stripped of their array-ness and be pointed at anything.

I'm not able to reproduce this error, could you give some code that actually causes it? The behavior of F$ is similar to undefined variables, which is probably what actually happened.