19

I'd like to make three arrays of the same length. According to the documentation, Arrays must be defined as int myArray[10]; where 10 can be substituted for a known length (another integer), or filled with an array {2, 3, 5, 6, 7}.

However, when I attempted to declare a value int arrSize = 10; and then an array based on that size int myArray[arrSize];, I get the following: error: array bound is not an integer constant.

Is there a way to variably determine array sizes, or do I just need to hardcode them? (I was taught hardcoding is bad and something to avoid at all costs.)

user3.1415927
  • 293
  • 1
  • 2
  • 5
  • I had a similar problem and did this. I am learning too so can't say if it is valid solution or not but it worked. See below part of code using vectors, it took me quite a bit to start to understand them and I am still not an expert by no means: #include #include #include #include #include <string.h> using namespace std; int main() { string name; string address; string town; string country; string answer; vector<vector> personData; for(;;) { vector myTempData; cout<<"enter name or n to exit"<<endl; getline(cin, name); if(name=="n") { bre – Misterxp Oct 25 '17 at 20:03

5 Answers5

29

Your question has 2 parts actually.

1/ How can I declare the constant size of an array outside the array?

You can either use a macro

#define ARRAY_SIZE 10
...
int myArray[ARRAY_SIZE];

or use a constant

const int ARRAY_SIZE = 10;
...
int myArray[ARRAY_SIZE];

if you initialized the array and you need to know its size then you can do:

int myArray[] = {1, 2, 3, 4, 5};
const int ARRAY_SIZE = sizeof(myArray) / sizeof(int);

the second sizeof is on the type of each element of your array, here int.

2/ How can I have an array which size is dynamic (i.e. not known until runtime)?

For that you will need dynamic allocation, which works on Arduino, but is generally not advised as this can cause the "heap" to become fragmented.

You can do (C way):

// Declaration
int* myArray = 0;
int myArraySize = 0;

// Allocation (let's suppose size contains some value discovered at runtime,
// e.g. obtained from some external source)
if (myArray != 0) {
    myArray = (int*) realloc(myArray, size * sizeof(int));
} else {
    myArray = (int*) malloc(size * sizeof(int));
}

Or (C++ way):

// Declaration
int* myArray = 0;
int myArraySize = 0;

// Allocation (let's suppose size contains some value discovered at runtime,
// e.g. obtained from some external source or through other program logic)
if (myArray != 0) {
    delete [] myArray;
}
myArray = new int [size];

For more about problems with heap fragmentation, you can refer to this question.

jfpoilpret
  • 9,132
  • 7
  • 37
  • 54
  • 4
  • ARRAY_SIZE = sizeof myArray / sizeof myArray[0];, this way you can change the type of myArray without introducing bugs. For the same reason, myArray = realloc(myArray, size * sizeof *myArray);. BTW, casting the return value of malloc() or realloc() is useless also. 2) Checking for myArray != 0 in the C version is useless, as realloc(NULL, sz) is equivalent to malloc(sz).
  • – Edgar Bonet May 20 '15 at 17:47
  • const int ARRAY_SIZE = 10; int myArray[ARRAY_SIZE]; Do you really think it is possible?. This would give variably modified array error in C. – Arun Joe Cheriyan Aug 06 '18 at 14:44
  • @ArunCheriyan in C I don't know, but in C++ it compiles and runs perfectly. Since Arduino is C++ based, then there is no issue here. – jfpoilpret Aug 06 '18 at 17:53
  • There could be one reason to use dynamic memory safely on an Arduino, which is when you DON'T delete that memory and reuse it anymore, so just for 'initialization' purposes. You just have to make sure that the total amount of dynamic memory is enough to leave space for other dynamic memory (including function/methods arguments which land on the stack). – Michel Keijzers Jun 15 '21 at 16:03