0

I have set up the AVR compiler for using with an Atmel microcontroller using this guide.

I don't have access to strings, vectors etc. How can this be added?

Friend of Kim
  • 850
  • 9
  • 24

2 Answers2

2

The quick answer is that they are not available and you need to write your own wrapper classes to get this sort of functionality.

If you want to use c++ for the embedded platform you won't have access to all of the standard library. Importantly though, you don't want all of the standard library as it's too heavyweight for some embedded projects. Some language features (like exception handling) might not be possible on the platform you are choosing or might be too expensive given the resources available to you. The lack of some language features makes it impossible to implement certain standard containers, for example the containers that can throw exceptions might not be able to be implemented in a standards-conforming way on some platforms. Additionally there's some c++ constructs that might be available but would be a bad idea to use on the embedded platform. Dynamic allocation of memory via new and delete will very likely run you into a significant number of problems as you don't have a lot of memory and issues such as memory fragmentation are very difficult to deal with. (you would probably want to look into placement new along with some other memory allocation scheme to avoid some of these issues if you needed dynamic memory for some reason)

If you want to have the benefits of containers like std::array and std::string you will need to write your own memory management classes. One of the main benefits of using the std containers is the way in which they greatly simplify your memory management (compared with using raw C-style-arrays). If you are doing a large embedded c++ project you can write your own wrappers for the memory management using RAII and other basic c++ language constructs. For the most part you need to avoid dynamic memory allocation and exception handling when making these classes.

One of the things I find has a good ROI is making some structs/classes that wrap an array along with the length of the array. By keeping the sizes connected you can keep your code a lot clearer. Frequently I find myself writing something like this:

template<typename T, uint8_t MAX_SIZE>
class array_helper{
    public:
        typedef T value_type;
        array_wrapper():
            m_data()
        {}

        T& operator[](unsigned int idx){
            return m_data[idx];
        }

        T* data(){
            return this->m_data;
        }

        const uint8_t s_max_size = MAX_SIZE;
    private:
        T m_data[MAX_SIZE];
};

You would want to expand on this to do what you need, but hopefully this gives you an idea.

shuttle87
  • 15,466
  • 11
  • 77
  • 106
1

do not do this.

using dynamic memory allocation on avr is not recommendable, since it has not a MMU and only very limited RAM and dynamic memory allocation requires some overhead for bookkeeping. also there is the danger of memory fragmentation.

on such tiny processors you should only use static and autmatic fixed size memory buffers. that ensures deterministic run time behavior.

vlad_tepesch
  • 6,681
  • 1
  • 38
  • 80
  • Okay, so the reason why the Arduino IDE includes this, is that it should be dead easy to work with? After all, Arduino is made to open microcontroller programming for a broader audience. – Friend of Kim Mar 31 '14 at 15:55
  • I am completely new to C++ and learn it because I want to make an octocopter as a hobby project. Can you really do without the STD? I mean, can you do everything without the STD on a microcontroller? – Friend of Kim Mar 31 '14 at 15:57
  • @FriendofKim the most things you will find for AVRs are C and thats for reason. using c++ gives a number of advantages in Code quality i think but you have to know which features of c++ you can use for free and which features comes with additional memory and runtime overhead. why do you require std::strings for a quadcopter software? – vlad_tepesch Mar 31 '14 at 16:04
  • Hmm, do you know any good books I should read? Almost every library for Arduino is written in C++, but they probably know what can be used for free... – Friend of Kim Mar 31 '14 at 16:06
  • The reason why I asked about the STD in the first place, is to do some testing. For example I want to test an LCD-screen. Then I want to write commands over serial, like "#clear#" to clear the screen. That's why I wanted strings. – Friend of Kim Mar 31 '14 at 16:07
  • Thank you for pointing out that it shouldn't be used for production code. But is it possible to include for convenience when just testing new components? – Friend of Kim Mar 31 '14 at 16:08
  • What about when communicating over serial, how can you do without the String type? – Friend of Kim Mar 31 '14 at 16:23