2

My C++ is seriously out-of-date, and I'm working on a project that will be juggling multiple timed actions at the same time, with a menu system to manage the interface and control those timed actions (A multi-channel power outlet controller.)

To do the control I'm planning to have an array of objects that get serviced every time through the event loop. I'm currently using a static C array of objects, but I'd like to use a dynamic C++ array where I can add and remove objects as needed.

I know that some features of the C++ standard library are not supported on Arduino. I think C++ calls smart arrays vectors, and believe vectors are part of the standard library, which isn't supported/fully supported on Arduino. Is there some sort of dynamic array class available?

2
  • github.com/maniacbug/StandardCplusplus Commented Jan 16, 2019 at 14:20
  • what for a dynamic array? take as static array the maximum you can/need. there is no other process that could use the memory. on a simple AVR MCU with no concurency, you can exactly plan the memory
    – Juraj
    Commented Jan 16, 2019 at 14:32

1 Answer 1

4

Even if it would be possible to use dynamic arrays, I would not recommend it, especially not on an Arduino Uno/Mega with only 2 or 8 KB memory.

Instead, create a static data element (e.g. array) that has a (reasonable) maximum number of created elements. This also has the benefit that the constructor will not be called after initialization, thus no variance in execution time (construction of objects can take time).

To manage multiple items within this data element, you can:

  • Using a ring buffer, this is the normal way to store items on one side, and process them from the other (so-called FIFO, first in first out).
  • Using a stack (for LIFO, last in last out).
  • For arrays, keep one variable that holds the number of elements currently filled.
7
  • Old-school C coding. Sure, I'm familiar with those techniques. I've been working too long on devices with tons of memory and plenty of processing power. It's been a long time since the days when I had to write apps in 8-bit assembler. These days I've come to rely on higher level constructs built into the language/OS. It's an adjustment for sure, and it's strange to have a language like C++ on a microcontroller. Causes some cognitive dissonance.
    – Duncan C
    Commented Jan 16, 2019 at 14:50
  • A simple singly linked list would work too.
    – Duncan C
    Commented Jan 16, 2019 at 14:55
  • @DuncanC I have the same issue ... being a software engineer, I think in lists, (dynamic) arrays, stacks, queues etc. A single linked list also is dynamic. Things get complicated when you need to insert elements in an existing list (and removing in some cases). Commented Jan 16, 2019 at 15:04
  • "Things get complicated when you need to insert elements in an existing list"? How so? Inserting and removing items seems easier with a list, not harder. Easier in some ways than a fixed array with empty flags for each entry
    – Duncan C
    Commented Jan 16, 2019 at 16:57
  • 1
    Actually, why bother with a boolean array? Just use NULL for empty slots, and check for NULL before acting on an entry in the array. Then I can add memory management that destroys objects as they are removed.
    – Duncan C
    Commented Jan 21, 2019 at 23:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.