Define an array of bytes depending on a variable

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Niro
Posts: 77
Joined: Mon Jan 03, 2011 8:58 pm
Has thanked: 29 times
Been thanked: 10 times
Contact:

Define an array of bytes depending on a variable

Postby Niro » Tue Jan 11, 2011 4:05 pm

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].
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

User avatar
Jan Lichtenbelt
Posts: 795
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Re: Define an array of bytes depending on a variable

Postby Jan Lichtenbelt » Tue Jan 18, 2011 9:14 am

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

Niro
Posts: 77
Joined: Mon Jan 03, 2011 8:58 pm
Has thanked: 29 times
Been thanked: 10 times
Contact:

Re: Define an array of bytes depending on a variable

Postby Niro » Tue Jan 18, 2011 9:25 pm

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

User avatar
Steve
Matrix Staff
Posts: 3111
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 89 times
Been thanked: 398 times
Contact:

Re: Define an array of bytes depending on a variable

Postby Steve » Wed Jan 19, 2011 7:43 am

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.
These users thanked the author Steve for the post:
Niro (Wed Jan 19, 2011 11:33 am)
Rating: 5%
 

Niro
Posts: 77
Joined: Mon Jan 03, 2011 8:58 pm
Has thanked: 29 times
Been thanked: 10 times
Contact:

Re: Define an array of bytes depending on a variable

Postby Niro » Wed Jan 19, 2011 11:38 am

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

User avatar
Benj
Matrix Staff
Posts: 14680
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4665 times
Been thanked: 4287 times
Contact:

Re: Define an array of bytes depending on a variable

Postby Benj » Wed Jan 19, 2011 11:39 am

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/
These users thanked the author Benj for the post:
Niro (Wed Jan 19, 2011 1:14 pm)
Rating: 5%