Shop OBEX P1 Docs P2 Docs Learn Events
Explain var declarations to me... — Parallax Forums

Explain var declarations to me...

mre_robmre_rob Posts: 31
edited 2007-05-14 15:33 in Propeller 1
When you declare a var as a byte, is that really just a way to say that the var starts at this memory location? I can see no way to define how many bytes the var should allocate. I discovered this during a byte parsing issue. Once I defined the var as an array of bytes, my overrun problem went away. I thought I read about var declarations in the spin manual, but maybe I missed this fact. Or is it assumed that since this is a low level language, most things are just pointers.

Also, are params passed into methods by reference or by value?

Is there a way to define a byte array var within the scope of a method? I am looking to isolate code, but I was not able to define byte arrays within the scope of a method.

My last one.... when should you use Dat vs. Var?

thanks...

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-14 14:30
    1) When you declare a variable as a byte, space for a single byte is allocated for the variable. If you want space for more than one byte, you have to declare a byte array and provide the size in the declaration. Spin is very similar to C in that subroutine calls can have value parameters only. That is, values are passed to a subroutine (32 bit integer values). If you want to pass anything else, you have to represent it as a value, thus addresses are passed to represent arrays (and strings) and floating point values (unlike in C where they're basic values) are passed around as 32-bit integers.

    2) Parameters are passed by value

    3) There's no way to define a byte array as a local variable since all local variables are long words (32-bit). There is a way to override the access to the local variable by using a ".WORD" or ".BYTE" suffix and you can follow this with an array subscript if you want. If you declare a local variable "X" as an array of 4 long words, you can use the area as a 16 byte array like "x.byte[noparse][[/noparse] i ]".

    4) VAR variables are allocated once for each instance of an object. DAT space is allocated only once for all instances. You'd put a variable into a DAT section if it is to be shared among all the instances of the object. If an object has only one instance, then it doesn't much matter where you put it although you can initialize a variable declared as a DAT. All VAR variables are initialized to zero when the program is loaded.
  • mre_robmre_rob Posts: 31
    edited 2007-05-14 15:33
    Mike Green said...
    1) When you declare a variable as a byte, space for a single byte is allocated for the variable. If you want space for more than one byte, you have to declare a byte array and provide the size in the declaration. Spin is very similar to C in that subroutine calls can have value parameters only. That is, values are passed to a subroutine (32 bit integer values). If you want to pass anything else, you have to represent it as a value, thus addresses are passed to represent arrays (and strings) and floating point values (unlike in C where they're basic values) are passed around as 32-bit integers.

    2) Parameters are passed by value

    3) There's no way to define a byte array as a local variable since all local variables are long words (32-bit). There is a way to override the access to the local variable by using a ".WORD" or ".BYTE" suffix and you can follow this with an array subscript if you want. If you declare a local variable "X" as an array of 4 long words, you can use the area as a 16 byte array like "x.byte[noparse][[/noparse] i ]".

    4) VAR variables are allocated once for each instance of an object. DAT space is allocated only once for all instances. You'd put a variable into a DAT section if it is to be shared among all the instances of the object. If an object has only one instance, then it doesn't much matter where you put it although you can initialize a variable declared as a DAT. All VAR variables are initialized to zero when the program is loaded.
    thanks Mike... It's starting to clear up!! I really appreciate your clarifying this for me.
Sign In or Register to comment.