On Monday 29 January 2007 06:59, ricardo tiago wrote:
> If i wanted to pass arrays of data between 2 components , what is the best
> way to this without having memory problems/or any kind of problems.
>
> The way i am doing:
> command uint8_t* Component1.getSomething(uint8_t size) {
> uint8_t *array=malloc(size*sizeof(*array));
> /* Fill array with something */
> return array;
> }
>
> In Component2:
> task void someName() {
> uint8_t* pointer;
> pointer = call Component1.getSomething(size);
> /* Do something with data */
> }
Most of the time, you can think of memory management as "owner allocates
storage". In your example, it appears that Component2 is the owner of the
data retrieved from Interface.getSomething(). I'd consider something like
this:
/* In Component1 */
command uint8_t Interface1.getSomething(uint8_t* array, uint8_t size)
{
/* Fill array with something, limiting to a max of 'size' bytes */
return /* the actual number of bytes written to array */;
}
/* in Component2 */
enum { MAX_NAMESZ = 16 };
uint8_t name[MAX_NAMESZ];
task void someName()
{
uint8_t len = call Interface1.getSomething(name, sizeof(name));
if (len) {
/* Do something with the data */
} else {
/* Handle the condition of no data */
}
}
If the size of the data that getSomething() must return is a constant that is
best associated with Interface1, then you can create Interface1.h and define
the size constant there, including this file into Component1 and Component2.
Steve
_______________________________________________
Tinyos-help mailing list
[email protected]
https://mail.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help