Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

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

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 lecture notes don't go into why.

Now i'm working on a moderate scale, embedded systems 'LURC' controller, mostly just takes advantages of the peripheral of the "Butterfly" demo board for the AVR169MEGA. produced 4 PWM signals to contol servo's and ESC. and also to provide an 9 seg LCD screen.

Now I can't think of anybetter way to store instructions as they are recieved vial USART serial, than a queue. esp for things where I'll need to wait until an unknown amount of data has been recieved: eg a string to display on the LCD screen.

so why don't you uses dynamic data structures on a microcontroller in a embedded systems? Is it just that you're on a heavily memory restricted enviroment, and have to be sure your mallocs are succeeding?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are a number of reasons not to use malloc (or equivalent) in an embedded system.

  • As you mentioned, it is important that you do not suddenly find yourself out of memory.
  • Fragmentation - embedded systems can run for years which can cause a severe waste of memory due to fragmentation.
  • Not really required. Dynamic memory allocation allows you to reuse the same memory to do different things at different times. Embedded systems tend to do the same thing all the time (except at startup).
  • Speed. Dynamic memory allocation is either relatively slow (and gets slower as the memory gets fragmented) or is fairly wasteful (e.g. buddy system).
  • If you are going to use the same dynamic memory for different threads and interrupts then allocation/freeing routines need to perform locking which can cause problems servicing interrupts fast enough.
  • Dynamic memory allocation makes it very difficult to debug, especially with some of the limited/primitive debug tools available on embedded system. If you statically allocate stuff then you know where stuff is all the time which means it is much easier to inspect the state of something.

Best of all - if you do not dynamically allocate memory then you can't get memory leaks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...