Is there a way to create your own Push() function?

Hey

I’ve been trying to get this to work, but it just wont.
Could anybody help me?

setup(){
    tryPushEx();
}

void tryPushEx(){
    int startArray[] = {0};
    startArray = Push(startArray, 0);
   
    //print the result 
    for(i = 0; i < sizeof(startArray); i++){
       Serial.println(startArray[i]);
    }
}

array Push(int startArray[], int value){
    int startSize = sizeof(startArray);
   
    // the new array is one bigger then before
    int newArray [startSize+1];

    //copy the old array into the new one
    for(int i = 0; i < startSize; i++){
        newArray[i] = startArray[i]
    }

    //add the last value to complete the "push"
    newArray[sizeof(newArray)] = value;
    return newArray;
}
    int startArray[] = {0};

An array with only one element.
Is that useful ?

 int startSize = sizeof(startArray);

startArray is a pointer.
A pointer has a fixed size…
You need to pass the size of the array into the function.

  return newArray;Returning variable that is about to go out of scope doesn’t make sense.

Even if the code would work, it would cause memory fragmentation and at some point crash the arduino. It could be done much simpler:

template <class DATATYPE, unsigned char SIZE> class PushPop {
private:
  const unsigned char m_Size = SIZE;
  DATATYPE m_Array[SIZE];
  int m_Index;
public:
  PushPop() { m_Index = -1; }
  bool CanPush() { return (m_Index < m_Size - 1); }
  bool CanPop() { return (m_Index > -1); }
  void Push(DATATYPE what) { m_Array[++m_Index] = what; }
  DATATYPE Pop() { return m_Array[m_Index--]; }
};

void setup()
{
  PushPop<int, 10> pp;
  pp.Push(10);
  pp.Push(20);
  pp.Push(30);
  Serial.println(pp.Pop());
  Serial.println(pp.Pop());
  Serial.println(pp.Pop());
}

The code is untested :slight_smile:

Danois90:
it would cause memory fragmentation

"Fragmentation" is a poor word choice.

A more reasonable word choice is "corruption"; as in "memory corruption". Which is likely to happen when Serial.println is called or an interrupt is serviced.

Hey

I need a way to push more data into an array by using a push function

Ex

I have a array filled with data with 5 slots, but i want to add an other one, therefor making it 6 slots. Since you cant remake the array without losing data to let it have 6 slots, I want a push function.

It pushes a value into a new slot.

Could anybody help?

Thanks in advance.

If the array eventually needs to hold up to N elements then why not declare it with N elements in the first place and only use as many of them you need at any one time ?

You have to decide in advance how many slots there are going to be, then declare a fixed array of that size. Then monitor how many things got pushed so that you don't run off the end of the fixed array.

Other memory constructs like a "linked list" might be more dynamic but anything dynamic has a risk of corrupting memory since the Arduino has no built-in protection for that kind of error.

...I want to add an other one, therefor making it 6 slots. Since you cant remake the array without losing data to let it have 6 slots, I want a push function.

There is, essentially, no such thing. The only way to an array "bigger" is to allocate a new, larger, array, and then copy all of the old data to the new array. This is obviously not-very-efficient, and requires that your system have at least twice the amount of RAM that is needed for the array (at some point, you have two copies.)
You can also implement data structures that are more complicated than arrays (and probably slower to access) - assorted varieties of linked lists and so on.
Some languages (Javascript, PDP) have a "push" operation on their "arrays" that behaves as you wish, but it is important to understand that this ends up implemented using one of the methods mentioned above (or a combination.)

In C, C++ its common to just use i++, i-- for pushing and popping via indexes. The next level up is
to define a template class, but that's quite a lot more complicated, for instance: https://www.dreamincode.net/forums/topic/314002-array-based-stack-with-template/