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
Define an array of bytes depending on a variable
Moderator: Benj
- Jan Lichtenbelt
- Posts: 797
- 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
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
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
-
- 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
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
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
- Steve
- Matrix Staff
- Posts: 3421
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
- Contact:
Re: Define an array of bytes depending on a variable
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.
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.
-
- 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
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
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
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Define an array of bytes depending on a variable
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/
Flowcode AVR uses Win AVR which is a release of the GCC compiler for WIndows designed for AVR devices.
http://winavr.sourceforge.net/
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel