在嵌入式系统上使用动态数据结构有多糟糕? [英] how bad is it to use dynamic datastuctures on an embedded system?

查看:81
本文介绍了在嵌入式系统上使用动态数据结构有多糟糕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在我明年参加的一个嵌入式系统部门中,我们将了解到动态数据结构在嵌入式系统程序中是一件坏事. 但是讲义并没有说明为什么.

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.

现在,我正在使用中等规模的嵌入式系统\'LURC'控制器,大部分只是利用了AVR169MEGA的蝴蝶"演示板的外围设备. 产生了4个PWM信号以控制伺服器和ESC.并提供9段LCD屏幕.

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.

现在,我想不出任何更好的方式来存储指令,因为它们是通过USART序列接收的,而不是队列. 尤其是在我需要等待直到收到未知数​​量的数据的情况下,例如在LCD屏幕上显示的字符串.

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.

那么,为什么不在嵌入式系统的微控制器上使用动态数据结构呢? 仅仅是因为您处于内存受限的环境中,并且必须确保您的malloc成功吗?

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?

推荐答案

在嵌入式系统中不使用malloc(或等效方法)有很多原因.

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.

这篇关于在嵌入式系统上使用动态数据结构有多糟糕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆