how bad is it to use dynamic datastuctures on an embedded system?

后端 未结 8 1560
深忆病人
深忆病人 2020-12-13 21:38

So in an embedded systems unit, that i\'m taking at uni next year, we will learn that dynamic data structures are a bad thing to have in an embedded system program. but the

8条回答
  •  温柔的废话
    2020-12-13 22:03

    Dynamic data structures on embedded systems are a bit like pointers in C++. Pointers (in C++) are evil. But sometimes they're the only option; sometimes they're the lesser evil; and sometimes it's OK to avoid them entirely. In cases where there is a good reason to use them, there can be "good" ways and "bad" ways to do this.

    Statically allocated variables and arrays are much faster to allocate and deallocate, and can be faster to access, than dynamically allocated data. See this answer.

    Dynamically allocated (by which I mean malloc()ed or similar) data also requires space overheads to keep track of the allocations. Several bytes per allocation at least - this space can be very valuable on embedded systems!

    Memory leaks are a massive problem on embedded systems, which can sometimes be expected to run for years. Avoiding dynamic allocation is prudent from this perspective.

    Embedded devices usually have fairly dependable specifications. You know what the transfer rate is, you know how fast you can deal with information, and so on. In your example, the solution is to use a fixed-size buffer as a circular queue. Make the buffer large enough to handle what your device needs to be capable of handling (and perhaps a tiny bit more). If too much data arrives, it's probably due to a fault or interference somewhere else anyway so there's not much point holding and trying to use all that data.

提交回复
热议问题