why?
Short answer is Arduino's have limited RAM.
Longer answer involves RAM shotgunning due to dynamic allocation. The less RAM you have, the more true that is.
Full answer requires experience with computers with less than or equal to 64K RAM using dynamic allocation vs planned fixed buffering over days to months long runs.
If you never use it, you have to plan ahead and limit scopes. That's not to say that dynamic allocation CAN'T work but I am saying that would be a harder trick and either way what you would run affects just how hard it will be to not crash in the long run. I wrote software that never came back for fixing when I used fixed allocation and I knew why that worked... it is easier to be sure of.
huh? this is static initialization not dynamic allocaiton
what is "shotgunning"?
the size is determineding by the compile, known when RAM is allocated, no different than defining any other variables
determining the size dynamically, rather than hardcoding it, allows easy expansion of the the feature, adding more pins in the case, at least minimizing the need to change code elsewhere.
When you use new and delete of variables, that is the dynamic allocation I learned about. When the deleted variable isn't the last one allocated, it leaves a hole. If the holes are not filled even as more variables are made by new, that is known as shotgunned. Call it what you want, it hastens the event when the stack meets the heap, or whatever you want to call that.
The stack is dynamic. I recommend against writing functions that call themselves on Arduino, whatever you want to call that. In fact I recommend to limit anything that builds up stack depth and to learn what does including some structures we indent code for, call those what you will since each one grows the stack until it is done.
We have to have it but we don't have to go deep even if it looks more contained.
Here's an example of a delayed blink without delay using a small state machine:
There's an element of dogma, especially when working on an ARM or ESP platform. Of course, with those platforms (since the standard C++ library is available), you should strive to use smart pointers such as std::unique_ptr.
why? what are "smart pointers"?
how do these comments help the OP?
static initialization is pretty basic, encourages data driven programs, making programs smaller and making the coding more problem domain specific
Agree. First, I should have said: " you should strive to use smart pointers such as std::unique_ptr when working with dynamically-allocated objects. Yes, you are correct, not the case here. But you did ask "why?" to:
Again, agree, not the case here.
But, to your question, Smart Pointers are a technique for managing dynamically-allocated objects in C++. Instead of writing:
auto objPtr = new Object;
You write:
auto smartObjPtr = std::unique_ptr<Object> (new Object);
Now when the lifetime of the object smartObjPtr ends, it will automatically call the destructor for the Object it points to and de-allocate the assigned memory, retuning it to the heap. This reduces the chances of a memory leak should you forget to do it yourself while using a regular pointer.
EDIT:
With C++14 you get std::make_unique:
auto smartObjPtr2 = std::make_unique<Object> ();
Back when I would do a lot of casting I can't easily get away with now (Borland TC++ let me pun like a muther), I'd have buffers and pointers and different classes to make temporary use of memory with mostly for read/write to file records in different or complex files.
That was when 64K was a lot of RAM. I kind of got used to it, wore that rut deep and made due with less RAM on custom apps boxes right up to Y2K.