I was wondering if arrays have the functionality of push and pop, or if there is another container which does. If the latter is true, could someone please direct me to a library with that functionality(I need push and pop)?
robotGenerator:
I was wondering if arrays have the functionality of push and pop, or if there is another container which does. If the latter is true, could someone please direct me to a library with that functionality(I need push and pop)?
Push and pop are just names for adding an element and take it off again. You only need to keep track of current position in array.
You are looking for a "stack" or FILO (first in, last out) data structure. The Boost library has it.
You can implement your own, as mentioned above, but you have to manage the container. For example, you have to keep track of the location of the top of the stack, and check for overflow/underflow.
I suggest you don't write Arduino code that dynamically alters the size of your array as it is easy to get corrupted memory in the small SRAM on an Arduino. Allocate an array with enough space in it and then you can place values into, or take them out to your heart's content.
...R
Perhaps you need to explain what you believe you mean by "push" and "pop".
Paul__B:
Perhaps you need to explain what you believe you mean by "push" and "pop".
He is meaning to put a new entry in to the array.
I think he is used to Javascript as it is a function of it.
With "array.push(value)" you put something into the array.
With "array.pop()" you remove the last value from the array.
I am also searching a way in Arduino to do that.
Grandmother:
He is meaning to put a new entry in to the array.
I think he is used to Javascript as it is a function of it.
With "array.push(value)" you put something into the array.
With "array.pop()" you remove the last value from the array.I am also searching a way in Arduino to do that.
This thread was over 3 years old. Probably should have started a new one.
But could you not do something like
int thisArray[] = {0, 0, 0, 0, 0, 0, 0};
int newNumber = 100;
void setup() {
}
void loop() {
for (int i = 6; i > 0; i--) {
if (i == 0) {
thisArray[i] = newNumber;
} else {
thisArray[i] = thisArray[i - 1];
}
}
}
If the push and pop are for a stack then you want a FIFO.
Grandmother:
I think he is used to Javascript as it is a function of it.
Javascript is an interpreted language that runs on a PC with 2 or more Gigabytes of ram. Concepts that work in that situation are not necessarily portable to an Arduino with 2k of SRAM.
...R
In Arduino it's not so complicated as to be beyond the hardware as long there is a limit to the array.
But is this for a FIFO stack or a LIFO buffer?
FIFO grows up or down, it has a const bottom pointer and a floating top pointer.
LIFO is circular, it has head and tail pointers.
Fact is, OP was a "one hit wonder" and never returned to answer my critical question,
It is an XY problem. We have absolutely no idea whatsoever what was in his little mind. ![]()
A total waste of time explaining an answer to an unknown question.
I suppose that you want to filter time data. This is useful for sensor projects. Push/Poke is used in C, VB, as, JS etc. It seems that Arduino would implement it because the ESP32/Arduino are often used with sensors.
As someone said below you can just write a little loop but it would be a good funcion.
Methinks, like "Grandmother", you are a little late to the party. ![]()
I think it points out a flaw in the new “suggested topics”.
It is unclear by which means these topics are selected, they are certainly not filtered for age…
I have clicked into suggested topics and found myself wondering WTF and coming across ppl I know have moved on, however, only to finally notice I’m reading an N year old thread.
“suggested topics” should be scrapped or at least made a wee bit smarter.
Yes, I do see that date and could filter them myself on that basis. Instead I just ignore them.
a7
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
