Shop OBEX P1 Docs P2 Docs Learn Events
How to access a variable that a pointer pointed to? /flexbasic — Parallax Forums

How to access a variable that a pointer pointed to? /flexbasic

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

  • evanhevanh Posts: 15,915

    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.

  • pik33pik33 Posts: 2,366
    edited 2023-06-23 16:00

    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) (or p(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:

    function expr(ct as integer) as expr_result, integer
      ...
      return t1,ct
    end function
    
    a,ct = expr(ct)
    

    do

    function expr(byref ct as integer) as expr_result
    ...
    a = expr(ct)  ' ct is updated to the new value by expr
    

    The byref will cause ct to be passed "by reference", which means internally a pointer to the variable will be passed and changes to ct inside the function will affect the caller's variable.

  • evanhevanh Posts: 15,915
    edited 2023-06-24 00:05

    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).

  • MicksterMickster Posts: 2,693
    edited 2023-06-24 11:36

    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

  • @Mickster said:
    Hope I'm not hijacking the thread but do we have code pointers in FlexBasic or have I missed it?

    We have pointers to functions/subroutines; is that what you meant?

    I realise that we have On-Gosub but is this a jump table?

    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
    :+1:

    Craig

Sign In or Register to comment.