Page 1 of 1

Define an array of bytes depending on a variable

Posted: Tue Jan 11, 2011 4:05 pm
by Niro
Hello,

I'm using Flowcode 4 for AVR pro and need to define an array of bytes depending on a variable 'i' e.g. MyArray.
I'm using this not waste precious memory if not needed. Additionally I'd like to free array elemnts/range if not needed anymore.
I'd appreciate to do this with Flowcode, because the array is accesed by the rest of the program.
Maybe it's possible to define/free the variable in C and somehow manage to show it in the Flowcode variables manager to access it.

Many thanks for any help!!

Niro

Re: Define an array of bytes depending on a variable

Posted: Tue Jan 18, 2011 9:14 am
by Jan Lichtenbelt
Dear Niro


I do not know if dynamic array declaration is possible in C or not.

But all variables and all array-elements needs a memmory place in the stack. On machine level it is only the stack what counts and not the name.
That means that instead of variables you can use only array elements.

Let make it more clear. E.g. you needs 5 variables like A,B,C,D and E and an array with 10 elements MyArray[9]. An alternative way is to declare MyArray[14] with 15 elements and use MyArray[10] instead of variable A, MyArray[11] instead of B etc.
You can make this dynamic by using just one variable Offset. Instead of MyArray[10] of the variable A, you write now MyArray[Offset+0] and for variable B: MyArray[Offset+1] etc, with Offset=10. Offset can be changed, but pay attention than the 'variables' will loss their values.

I think in this way you can use a dynamic array declaration in Flowcode. But array MyArray has to be declared the first time with the maximum number of elements. Also this way of programming will take some additional programming steps, because you have to calculate all the time Offset+i.

Succes

Jan Lichtenbelt

Re: Define an array of bytes depending on a variable

Posted: Tue Jan 18, 2011 9:25 pm
by Niro
Daer Jan,

many thanks for your answer!
As I understand you always need to declare the maximum array that might be used (e.g. MyArray[15]), even if you actually only need an array with 7 elements and you're not sure if you might need more later. I think your suggestion doesn't really save memory if actually not needed, it only reduces the number of variable names. Is that right?
Maybe it's a way not to use global variables but more local variables with macros. This would be kind of a dynamic memory usage.

Best regards,
Niro

Re: Define an array of bytes depending on a variable

Posted: Wed Jan 19, 2011 7:43 am
by Steve
Flowcode does not allow dynamic memory allocation natively, but the underlying C compiler can. BoostC uses "alloc", and HITECH will use something similar - search in the help for the compiler for more information.

Of course, because you need to do this with C code embedded into the flowchart, it does mean that you will not be able to simulate this allocation of memory in the program.

Re: Define an array of bytes depending on a variable

Posted: Wed Jan 19, 2011 11:38 am
by Niro
Many thanks, Steve!

Having a look at BoostC and Hitec, both seem to be compiler for PIC. Looking into the compiler options, there's only an avra.bat.
How can I find out, which compiler is my Flowcode for AVR using?

Thanks in advance,
Niro

Re: Define an array of bytes depending on a variable

Posted: Wed Jan 19, 2011 11:39 am
by Benj
Hello Niro,

Flowcode AVR uses Win AVR which is a release of the GCC compiler for WIndows designed for AVR devices.

http://winavr.sourceforge.net/