Arrays as a Parameter
kt88seamp
Posts: 112
Is there a way to pass an array as a parameter between methods? If not a pointer?
Comments
Several of the example apps do this...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
My Prop Info&Apps: ·http://www.rayslogic.com/propeller/propeller.htm
My Prop Products:· http://www.rayslogic.com/Propeller/Products/Products.htm
THE ONLY THING YOU CAN PASS AS PARAMETER IS A LONG! (or more)
What the long means can be different. Sometimes it's a byte value or a word value or a long value. You can also interpret it as a 4 byte array, you can interpret it as a 2 word array .. you can use it as a 2 byte 1 word 'container'.
If you have bigger data structures there is no other way than passing the address to that data structure. Which by the way is far more efficient, because each parameter is pushed to the stack. This needs additional memory plus runtime.
As a trivial example, this method can sums the first n elements of the array passed to it; it assumes the array is of bytes.
Let's say you have a 10-element array of bytes and you want to know the sum of the first five elements, you can call the above method like this:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA
Post Edited (JonnyMac) : 5/1/2010 3:26:41 PM GMT
1. It's easy ... a parameter is a LONG - period.
2. Whatever fits into a long can be passed directly. Up to 4 bytes, up to 2 words, 1 long. Whatever they contain.
3. AND yes, Jonny, both sides have to know what a parameter contains/has to contain
Basically there are only 2 posibilities: either the parameter contains a value or an address.
If you use an object you need to know what the content of a function parameter has to be - an address or a value. Otherwise the function won't work as expected. If the function returns a value the caller needs to know what that function will return, a value or an address.
For example:
- a:=string( "jlkjj" ) is a function buildin the SPIN compiler, but it will "give back" an address to the string.
- num.tostr( 10 ) is a function which expects a long and it returns an address to the converted string.
- strsize( @a ) is a function expecting a string address and gives back a long
But that's only the easy part ;o) I tend to squeeze out any byte of a parameter/return value. Which means I often use one parameter to pass several values or a mixture of pointer and value .....
Are they passing between cog's?
If not, you can just define the array in the 'VAR' section of your code and it will be accessable to all of the PUB/PRI code in the block.
J-
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Signature space for rent!
Send $1 to CannibalRobotics.com.
Are they passing between cog's?
If not, you can just define the array in the 'VAR' section of your code and it will be accessable to all of the PUB/PRI code in the block.
J-
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Signature space for rent!
Send $1 to CannibalRobotics.com.
I am passing the array to the transmit method with a pointer. Is the best way? It is definitely the easiest.
How many bytes total·are in the array?
J-
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Signature space for rent!
Send $1 to CannibalRobotics.com.
Is the [noparse][[/noparse] ] missing in the definition of DataBytes (behind the main)? If not, DataBytes would only be one long and your assignments will overwrite some memory that does not belong to DataBytes. If it's in the main COG you have a good chance that nothing will happen, but if you do this in a function called by COGNEW and the stack is just big enough for some parameters / local variables, you have a good chance to overwrite some important stuff.
Now let's assume that your DataByte only has to store bytes. You should not create that variable as a local variable, as these are always LONGs. Better put the DataByte to the VAR section in this case, really using bytes.
VAR
byte DataBytes[noparse][[/noparse] 16 ]
If your DataBytes are constant, then you can also think of creating DataBytes in a DAT section, because there the assignment is for free (no extra menory, no extra runtime):
DAT
DataBytes byte 45, 67, 89, 34, ....
In transmit array you don't want to send the address of the array, do you? You want to send the bytes in the buffer, so you have to get the values from that address:
PUB transmit( dataBytes_adr )
repeat i from 0 to 3
SER.TX( byte[noparse][[/noparse]dataBytes_adr]
It's a good idea to give a hint of the content of a variable by using a good name. _adr means that this variable only contains an address to a variable.