7

I'm working on an embedded application in which I'd love to use a container like std::vector<>. Unfortunately I must not use the heap. So std::vector<> could not be used. So I'm looking for an alternative.

I've seen boost static_vector but the boost approach seems too heavy for the microcontroller as far as I've seen. Or are there any experiences using boost on a small microcontroller (for example only the static_vector?)

One assumption could be made: the maximum number of entries during the whole application runtime is known at compile time.

So I'm wondering if there is any open source solution for this or if I have to implement a container by myself which is based on the std::array<> implementation and adds some logic to enable the following operations:

Add (push_back()) and remove (erase()) elements during the runtime. Providing the typical container iterators and a random access. Also the short hand for ( : ) loop should be available.

So my naive approach would be:

  • Providing the iterators, and the random access seems easy to me, and should be mostly based on the std::array<> functions

  • Adding the add (push_back) and remove (erase) should be no problem with some logic.

  • But how is the for ( : ) loop support to implement?

  • Are there any other things I need to consider?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Andreas
  • 343
  • 1
  • 4
  • 13
  • 1
    For c++ it would make more sense to talk about [storage duration](http://stackoverflow.com/a/408691/1460794) than heap vs. stack. – wally Jan 12 '17 at 20:02
  • A Boost circular buffer with static capacity seems to fit the bill. – Kerrek SB Jan 12 '17 at 20:04
  • @Muscampester In principal you're perfectly right but we're talking here about some kind of holy grail of the embedded world (at least in my company) in which heap is deuced, no matter whether it's allocation remains constant during runtime or not. – Andreas Jan 12 '17 at 20:05
  • 1
    Could you please re-phrase that? "in which heap is deuced, no matter whether it's allocation remains constant during runtime or not" made no sense to me. – Jesper Juhl Jan 12 '17 at 20:10
  • 1
    Why not just use a C-style static array? – Barmar Jan 12 '17 at 20:12

1 Answers1

8

You can create a std::vector with a custom std::allocator that for instance returns pointers from a static buffer.

Rereading your question, you say that the total number of entries inside the container, is known at compile time, why not use an std::array then or even a good old fashioned array?

As for the range based for loop (for ( : )) it simply requires the begin and end member or free functions to be defined for the type, so if you do create your own type, you need to define these.

Danra
  • 9,546
  • 5
  • 59
  • 117
Tommy Andersen
  • 7,165
  • 1
  • 31
  • 50
  • The thing is, that the number of elements is dynamic, based on the device in which the software is finally used in. The only thing I know is how much elements the container has to hold at maximum (plus some reserve). So let's assume one device has four sensors and the other one seven. I'd like to manage them in a vector rather than in an array. I could do this in old fashioned arrays or std::array<> as well but I don't like this approach. – Andreas Jan 12 '17 at 20:09
  • 3
    @Andreas Here's an example of a custom allocator which is close to what you're looking for (it does a bit more but you can probably simplify it to suit your needs) https://howardhinnant.github.io/stack_alloc.html – Danra Jan 12 '17 at 20:33