global array of unknown size

Hello I have the following problem:

I am transmitting data from a serial interface to the arduino.

There shall be a global array which shall contain the data.

The problem is, previously I do not know how much data will get transmitted - I do not know how much elements the array shall have.

Is there a way, to redim the array at runntime?

The data that the array shall contain is signed integer and I am using the arduino due, is there a way to say how much array elements the arduino due can handle bevore it has a problem with the RAM?


No you can't re-size the array at runtime to did to work out/find out the amount of data you need to store.

OR use the data as it comes in,

OR use the the heap in some way to store it

You know the amount of ram the due has, you know the sizeof an int and you should have some idea of that the rest of your program requires after that its easy.


A Due has 96KB of RAM. An integer requires four* bytes to store. If you leave a few KB left over for the rest of your program, you could probably store 20,000 or so integers.

When dealing with large datasets, a common strategy is buffering. Define a buffer of a 128 or 1024 or so integers, fill the buffer with incoming data, process the data in the buffer, then overwrite the buffer with new incoming data and repeat.

If you must have all of the data before processing and therefore cannot buffer, then just allocate an array of the largest size you ever expect to receive. If you receive less, oh well you wasted some RAM but otherwise no harm done. Just increment a counter as you receive your data, so you know where the end of your data is in your array.

* Four bytes on a Due. A regular Arduino stores an int in two bytes.

Do you know the upper limit of how much data can come?

You can set up a pointer to integer as global and allocate memory at run time then set the pointer to point at that. I would also make a global variable to save the number of ints in the array.

It is up to you (in your code) to make sure the incoming data starts with size of the array and that the array will not be too big.

An integer requires four* bytes to store.

An integer requires from one to eight bytes to store ("byte" to "long long")
On a Due, an "int" requires four bytes.