C++ usage in embedded systems

后端 未结 17 1501
南旧
南旧 2021-01-30 22:45

What features of C++ should be avoided in embedded systems?

Please classify the answer by reason such as:

  • memory usage
  • code size
  • speed<
相关标签:
17条回答
  • 2021-01-30 23:26

    If you are using a development environment targeted toward embedded development or a particular embedded system, it should have limited some of the options for you already. Depending on the resource capabilities of your target, it will turn off some of the aforementioned items (RTTI, exceptions, etc.). This is the easier route to go, rather than keeping in mind what will increase size or memory requirements (although, you should get to know that mentally anyway).

    0 讨论(0)
  • 2021-01-30 23:26

    For embedded systems, you'll predominately want to avoid things that have a definite abnormal runtime cost. Some examples: exceptions, and RTTI (to include dynamic_cast and typeid).

    0 讨论(0)
  • 2021-01-30 23:28

    time functions are usually OS dependent (unless you rewrite them). Use your own functions (especially if you have a RTC)

    templates are ok to use as long as you have enough space for code - othwerise don't use them

    exceptions are not very portable also

    printf functions that don't write to a buffer are not portable (you need to be somehow connected to the filesystem to write to a FILE* with printf). Use only sprintf, snprintf and str* functions (strcat, strlen) and of course their wide char corespondents (wcslen...).

    If speed is the problem maybe you should use your own containers rather than STL (for example the std::map container to make sure a key is equal does 2 (yes 2) comparisons with the 'less' operator ( a [less than] b == false && b [less than] a == false mean a == b ). 'less' is the only comparison parameter received by the std::map class (and not only). This can lead to some performance loss in critical routines.

    templates, exceptions are increasing the code size (you can be sure of this). sometimes even performance is affected when having a larger code.

    memory allocation functions probably need to be rewritten also because they are OS dependent in many ways (especially when dealing with thread safety memory allocation).

    malloc uses the _end variable (declared usually in the linker script) to allocate memory but this is not thread safe in "unknown" environments.

    sometimes you should use Thumb rather than Arm mode. It can improve performance.

    So for 64k memory I would say that C++ with some of its nice features (STL, exceptions etc) can be overkill. I would definitely choose C.

    0 讨论(0)
  • 2021-01-30 23:29

    The document "Information technology — Programming languages, their environments and system software interfaces — Technical Report on C++ Performance" gives also some good informations about programming in C++ for an embedded device.

    0 讨论(0)
  • 2021-01-30 23:29

    In most systems you do not want to use new / delete unless you have overridden them with your own implementation that pulls from your own managed heap. Yes, it'll be work but you are dealing with a memory constrained system.

    0 讨论(0)
提交回复
热议问题