Shop OBEX P1 Docs P2 Docs Learn Events
Variables vs Pointers and Buffers vs Temporary Memory Storage — Parallax Forums

Variables vs Pointers and Buffers vs Temporary Memory Storage

turbosupraturbosupra Posts: 1,088
edited 2011-09-29 19:08 in Propeller 1
Hello,

Can someone logically layout the differences here, I'm having trouble wrapping my mind around this. When is it appropriate to use a pointer, and when to use a variable, when writing spin code? Why would a function take an address/pointer to a variable instead of the variable itself? What is the advantage to this?

I also have a question about buffers and temporary memory storage. If I write a parsing method that parses different (but similar) sets of data repeatedly ... after a value is returned, how can I clear out all of the temporary memory locations that parsing method will store data in before returning its final value?

Thanks for reading.

Comments

  • Heater.Heater. Posts: 21,230
    edited 2011-09-29 12:20
    Well firstly if you pass a variable to a method the method gets a copy of that variable. The method can read that copied value of course. But if it writes to it only the methods local copy is written to. The original value in the caller is unchanged. This is known as "pass by value".

    But what if you want the callers variable to be updated by the method? Then you can pass the address of the variable to the method as a parameter. The method can then read and write to that address. The callers variable is then updated as required. This is known as "pass by reference".

    Also, what if you want to pass an array as a parameter to a method. In Spin one cannot pass an array by value. That would require copying the entire array somewhere for the method to use. Instead one has to give the address of the array as the parameter. Pass by reference.

    Further. What if you want the method to be able to update a structure of different variables which all have different names in the caller. Well if they are declared in sequence then the address of the first one can be passed to the method. The method can then update any or all of the variables in the structure by using offsets from the passed address. Only one parameter is required then instead of one per structure element.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-09-29 12:31
    You can use ordinary global variables as temporary storage (VAR section) or you can declare variables in the PUB / PRI declaration as local variables. The VAR section variables are normally initialized to zero when the program is loaded, but the PUB / PRI local variables are not initialized. You have to do that yourself. Typically this is done when the method with the local variables is entered, as one of the first couple of statements. Since the local variables for any given method are all longs and allocated in the order they're declared, you can easily initialize them all to zero using LONGFILL. If the first variable is called X and there are 3 of them, you'd use LONGFILL(@X,0,3).

    It's much better to initialize variables before their first use rather than re-initialize them after their last use. It's usually much easier to guarantee that they're initialized properly before they're used than to guarantee that you've anticipated all the ways they might be used last and re-initialize them in each case.
  • turbosupraturbosupra Posts: 1,088
    edited 2011-09-29 19:03
    Hi Heater, thanks for the reply.

    So pass by value (PUB method(value) ) can be used if I want the original variable to remain unchanged, say if I was calling it by 2 different methods for different purposes, but want to return manipulated value(s)?

    And pass by reference (PUB method(@reference) ) can be used if I want to modify the original value when I return something?

    The array example makes sense, I haven't ever needed to uses structs, but I believe I understand your example there. Thank you

    Heater. wrote: »
    Well firstly if you pass a variable to a method the method gets a copy of that variable. The method can read that copied value of course. But if it writes to it only the methods local copy is written to. The original value in the caller is unchanged. This is known as "pass by value".

    But what if you want the callers variable to be updated by the method? Then you can pass the address of the variable to the method as a parameter. The method can then read and write to that address. The callers variable is then updated as required. This is known as "pass by reference".

    Also, what if you want to pass an array as a parameter to a method. In Spin one cannot pass an array by value. That would require copying the entire array somewhere for the method to use. Instead one has to give the address of the array as the parameter. Pass by reference.

    Further. What if you want the method to be able to update a structure of different variables which all have different names in the caller. Well if they are declared in sequence then the address of the first one can be passed to the method. The method can then update any or all of the variables in the structure by using offsets from the passed address. Only one parameter is required then instead of one per structure element.
  • turbosupraturbosupra Posts: 1,088
    edited 2011-09-29 19:08
    Mike, that's a great point, thank you.

    Mike Green wrote: »
    You can use ordinary global variables as temporary storage (VAR section) or you can declare variables in the PUB / PRI declaration as local variables. The VAR section variables are normally initialized to zero when the program is loaded, but the PUB / PRI local variables are not initialized. You have to do that yourself. Typically this is done when the method with the local variables is entered, as one of the first couple of statements. Since the local variables for any given method are all longs and allocated in the order they're declared, you can easily initialize them all to zero using LONGFILL. If the first variable is called X and there are 3 of them, you'd use LONGFILL(@X,0,3).

    It's much better to initialize variables before their first use rather than re-initialize them after their last use. It's usually much easier to guarantee that they're initialized properly before they're used than to guarantee that you've anticipated all the ways they might be used last and re-initialize them in each case.
Sign In or Register to comment.