Pages: [1]   Go Down
Author Topic: What is the limit for array size on Arduino DUE?  (Read 1108 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26509
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could do the initialisation in "setup" with a memset or a memclr.
Logged

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 642
Posts: 50362
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 223
Posts: 13910
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Logged

Rob Tillaart

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

Offline Offline
Jr. Member
**
Karma: 3
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26509
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think the cleanest way to declare an array's size is using a "const unsigned long"
Logged

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

Offline Offline
Jr. Member
**
Karma: 3
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 223
Posts: 13910
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.



Logged

Rob Tillaart

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

Offline Offline
Jr. Member
**
Karma: 3
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you robtillaart for that clarification
Logged

The Netherlands
Offline Offline
Full Member
***
Karma: 1
Posts: 143
MKDS hacker and Programmer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

IIRC I have used even bigger arrays. Like 93000 or something.
Logged

Subscribe my youtube channel: http://www.youtube.com/user/MKDS3

Pages: [1]   Go Up
Jump to: