Using C++ in an embedded environment

前端 未结 11 1247
执笔经年
执笔经年 2021-01-02 17:43

Today I got into a very interesting conversation with a coworker, of which one subject got me thinking and googling this evening. Using C++ (as opposed to C) in an embedded

11条回答
  •  粉色の甜心
    2021-01-02 18:25

    C++ is suitable for microcontrollers and devices without an OS. You just have to know the architecture of the system and be conscious of time and space constrains, especially when doing mission critical programming.

    With C++ you can do abstraction which often leads to an increased footprint in the code. You do not want this when programming for a resource-limited machine such as an 8-bit MCU.

    Generally, avoid:

    • Dynamic memory allocation because it represents uncertainty in timing
    • Overloading
    • RTTI because the memory cost is large
    • Exceptions because of the execution speed lowering

    Be cautious with virtual functions as they have a resource cost of a vtable per class and one pointer to the vtable per object. Also, use const in place of #define.

    As you move up to 16 and 32-bit MCUs, with 10s or 100s of MB RAM, heavier features like the ones mentioned above may be used.

    So to round up, C++ is useful for embedded systems. A main benefit is that OOP can be useful when you want to abstract aspects of the microcontroller, for example UART or state machines. But you may want to avoid certain features all of the time and some of the features some of the time, depending on the target you are programming for.

提交回复
热议问题