Variables vs Pointers and Buffers vs Temporary Memory Storage
turbosupra
Posts: 1,088
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.
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
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.
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.
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