Go Down

Topic: What is the limit for array size on Arduino DUE? (Read 1 time) previous topic - next topic

petrv

Hi, I am trying to create 4KB array to store data, but I am not very lucky with that. When writing

byte arr[4096] = {0};

I get error:

ArduinoSecurityAccess:21: error: too many initializers for 'byte
  • '

    While when skipping the explicit initialization, the program compiles, however, it is not running correctly. The array is declared outside the setup() or loop(). By trial and error, I found out that array size 1023 bytes is okay, but I start getting errors using array of size 1024 bytes.

    I had an impression that Arduino Due has 96KB of memory and an array of this size should not be a problem. Am I doing something wrong? How can I get that large array to work?

    Thanks

AWOL

You could do the initialisation in "setup" with a memset or a memclr.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Quote
I had an impression that Arduino Due has 96KB of memory and an array of this size should not be a problem.

Seems right to me.

Quote
Am I doing something wrong?

Well, you didn't post all of your code.

Quote
How can I get that large array to work?

You need to define what doesn't work.

Global arrays are initialized to 0 by default. So, supplying an initializer to initialize one element of the array to the default value doesn't seem useful.

It might be, and I'm just guessing here, that the compiler doesn't properly initialize arrays that large. You could put a for loop in setup() to initialize it.

petrv

Thanks for all your answers, but I found the problem already, and it is far more simple. Luckily, the compiler does a good job. unluckily, I am idiot, because I actually did not posted the exact code I was using, which was:

byte size = 4096;
byte array[size];

I missed that size overflowed, therefore initializing empty array - that is why it crashed while trying to initialize it explicitly. And that is the reason while with size = 1023 it worked, since it interpreted it as 255.

I feel ashamed.

robtillaart

Quote
I feel ashamed.


The man with insight enough to admit his limitations comes nearest to perfection.
Johann Wolfgang von Goethe
Read more at http://www.brainyquote.com/quotes/keywords/admit.html#PPhdA36uquEmBtI0.99


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

gallegojm

I think the cleanest way to declare an array is using a #define directive:
Code: [Select]
#define ARRAY_SIZE 65536   // maximum for Due
uint8_t array[ ARRAY_SIZE ];


AWOL

I think the cleanest way to declare an array's size is using a "const unsigned long"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

gallegojm

Well, my arguments in favor of #define are:
1- It is a constant that can not be modified later in the program
2- It does not waste ram memory
A constant variable meets the first point
I am not sure if it meets the second one?
Is there another argument in favor of a constant variable?

robtillaart

The compiler optimizes const variables so they do not use RAM either.

a    #define   can be used for conditional code further in the program. That is something a   const    cannot do.

e.g if the  ARRAYSIZE  is not defined all the code to store sensor samples in an array and do processing on it is removed.
Only the live processing part of the code will be available.



Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

gallegojm

Thank you robtillaart for that clarification

Gericom


I think the cleanest way to declare an array is using a #define directive:
Code: [Select]
#define ARRAY_SIZE 65536   // maximum for Due
uint8_t array[ ARRAY_SIZE ];



IIRC I have used even bigger arrays. Like 93000 or something.
Subscribe my youtube channel: http://www.youtube.com/user/MKDS3

Go Up