Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top