How to access a variable that a pointer pointed to? /flexbasic
pik33
Posts: 2,387
There is an example in documentation:
var x = new ubyte(10) ' allocate 11 (not 10) bytes and return a pointer to it x(1) = 1 ' set a variable in it
How to
var x = new ubyte ' allocate 1 byte and return a pointer to it (????)= 1 ' set a variable in it
?
Comments
Why not just use a single compile time declared variable? The usual reason to allocate from heap or stack is for large runtime filled arrays.
This question is the side effect of problems with passing and returning classes and multiple valuse to/from functions. I tried a workaround involving passing/returning a pointer to temporary allocated variable, then I realized that I don't know how to access a variable having a pointer in FlexBasic (in normal way, not with acrobations using poke and cast, or treating a variable as one-element array). In C you use * to do this, in Pascal there is ^, something should be here, too.
Dereferencing a pointer in BASIC is always done with array notation, like
p(0)
(orp(1)
if you have set 1 as the default array base).However, for passing variables to subroutines and functions it's much easier to use
byref
parameters than pointers. That is, instead of:do
The
byref
will causect
to be passed "by reference", which means internally a pointer to the variable will be passed and changes toct
inside the function will affect the caller's variable.Ah, right. This is why Chip made Spin without type checking. He much preferred assembly's freewheeling over C's myriad of referencing syntax (and presumably Pascal's too).
Hope I'm not hijacking the thread but do we have code pointers in FlexBasic or have I missed it?
I realise that we have On-Gosub but is this a jump table?
Craig
We have pointers to functions/subroutines; is that what you meant?
Yes, on-gosub and on-goto (and select/case) are implemented internally with a jump table, if appropriate (the compiler tries to guess whether a jump table or series of if statements would be more efficient).
@ersmith
Oh the select/case is interesting...I'll test it
Craig