Defining an array of indefinite size

Hello,
I am currently working on a piezoelectric sensor for a wheel balancing machine and I am acquiring its real time values. I want to define an array where I can store these values because later on I’d be needing those values to figure out the maximum value from that array.
I am having issues regarding declaring/defining array of an indefinite size.Since I will be acquiring a lot of values and I find no point of defining its size before acquiring values.
Is there anyway I can define and store values in an array without defining its size?

Do you really need an array? If you only need to know the maximum, you can just keep the highest so far in a variable say maxSoFar. If the next reading is lower, do nothing; if next reading is higher, overwrite it. And if you need to know that it was the Nth reading, just keep a counter and put that value into another variable say maxSoFarPosition whenever you update the maximum, to keep maxSoFar and maxSoFarPosition in synch.

But to answer your question, I think, but am by no means certain, you have to declare it big enough up front, which means you have to have some idea of how many readings you will take, then add a bit extra for luck.

Thank you,
I think that will work too.
I guess you mean this?

int x=A0;
float maxsofar=0;
void setup()
{

  • Serial.begin(9600);*

}
void loop()
{
float z=analogRead(x);
if ( z> maxsofar )
*{ *

  • maxsofar=z;*
  • Serial.println(maxsofar);*
    *} *

}

HarisIftikhar:
I guess you mean this?

Zakly

HarisIftikhar:
Hello,
I am currently working on a piezoelectric sensor for a wheel balancing machine and I am acquiring its real time values. I want to define an array where I can store these values because later on I'd be needing those values to figure out the maximum value from that array.
I am having issues regarding declaring/defining array of an indefinite size.Since I will be acquiring a lot of values and I find no point of defining its size before acquiring values.
Is there anyway I can define and store values in an array without defining its size?

If you want to implement a dynamic array, which is essentially what you are talking about, then you will probably have to write your own class to implement one.

It is not difficult if you have a good grasp of OO programming and operator functions etc.

Unless you can find a library on Github that implements a dynamic array.

Be aware that extensive use of the heap on Arduino is not recommended due to heap fragmentation and resulting sketch instability when it runs over days to weeks.

@wilfredmedlin... are you in touch with Zackly?
Please pass my details on to him. :wink:

This thread is a very good example of why it's important to say why one wants to do something, since in this case it seems my solution to the OP's properly explained problem was acceptable, and was not the solution that s/he had contemplated and asked for help on.

Karma++ for explaining the problem.

lastchancename:
@wilfredmedlin... are you in touch with Zackly?
Please pass my details on to him. :wink:

Or her :wink:

Damn. Got me as an old sexist fart.

boylesg:
If you want to implement a dynamic array, which is essentially what you are talking about, then you will probably have to write your own class to implement one.

It is not difficult if you have a good grasp of OO programming and operator functions etc.

Unless you can find a library on Github that implements a dynamic array.

Be aware that extensive use of the heap on Arduino is not recommended due to heap fragmentation and resulting sketch instability when it runs over days to weeks.

It's not like this is an esoteric and arcane data structure that Indiana Jones needed to excavate out of the Temple of Doom. A vector (resizable array) is an extremely commonly-used data structure that is part of the C++ Standard Template Library. The ArduinoSTL library is not hard to find.

Jiggy-Ninja:
It's not like this is an esoteric and arcane data structure that Indiana Jones needed to excavate out of the Temple of Doom. A vector (resizable array) is an extremely commonly-used data structure that is part of the C++ Standard Template Library. The ArduinoSTL library is not hard to find.

Could you please explain the advantages of using a variable sized array against declaring it at its maximum size and subsequently using the portion required at the time ?

Not this again.

UKHeliBob:
Could you please explain the advantages of using a variable sized array against declaring it at its maximum size and subsequently using the portion required at the time ?

For an Arduino project, probably very little advantage. But that's not what I was talking about. I was specifically rebutting boylesg's assertion that "you will probably have to write your own".

The obvious data structure, if the ultimate size of the array is not known in advance, would be a linked list…

Regards,
Ray L.

It is generally advisable to not use dynamic memory allocation on the Arduino and similar platforms with extremely limited RAM. Instead, you should allocate an array large enough to fit the highest number of readings you plan to take. That way, you can make sure that you won't run out of memory (if it's a local variable, you have to manually add the size of it to the number reported by the IDE on compile, if it's static or global, it will be included).

The moment you start using dynamic memory allocation, it's much harder to know ahead of time whether you'll run out of memory under a given set of circumstances, and running out of memory is almost never handled gracefully.

Furthermore, when you're frequently allocating and deallocating memory with malloc()/free() you can end up fragmenting the free memory, such that you experience symptoms of running out of memory (failure to allocate a requested block of memory, or stack/heap collision) while there is still free memory, just in the wrong locations (this is why the antipattern of pulling data from serial and appending to a String is so good at provoking problems). Since there's so little memory on the Arduino, situations like this can occur very easily - and you don't know whether your program is vulnerable to this ahead of time, because the dynamic allocation occurs at runtime, depending on operating conditions.

This is not to say that there's never a correct time for dynamic memory allocation on the Arduino, only that there are very few correct times for it; without some radical revelation from OP, it sounds like this isn't one of them.