当前位置:网站首页>Embedded development: five C features that may no longer be prohibited
Embedded development: five C features that may no longer be prohibited
2022-06-30 13:33:00 【Guangdong embedded Education】
Best practices and industry standards have evolved over time , But they represent snapshots of guiding wisdom . Although technological advances have invalidated previously known best practices , But best practices can develop slowly , And often become entrenched . Used in the field of embedded development C Linguistic features have suffered the same fate . Many best practices originate from 80 Age and 90 years , The compiler was still weird , And microcontrollers do have limited resources . From then on , Compilers and microcontrollers have come a long way , many “ Forbidden ” Features and design techniques may no longer be the case .
features 1 – Float
Used in embedded systems Float It has been strongly opposed for a long time .Float Traditionally, there are many potential sources of problems . First , The microcontroller does not have a floating point unit . in the majority of cases , For the lowest end microcontroller , This is still true today , But it contains floating-point units (FPU) The cost has dropped significantly to mid-range and general purpose microcontrollers have begun to include FPU The degree of . Advances in microcontroller technology now include hardware acceleration of mathematical functions , Even without FPU It can also help speed up the calculation .
secondly , In the absence of FPU In case of use float The compiler is required to introduce a large and slow software library . since 20 Since the late 20th century , Compiler technology has been greatly improved , Even in software 8 Floating point calculations performed on bit microcontrollers are also optimized to a negligible degree . Technology is changing , Embedded developers need to change .
features 2–malloc
Malloc Allows developers to dynamically allocate memory during program execution , If used improperly in embedded systems , It can be a very dangerous tool . Traditionally , It is forbidden to use in the system with limited resources malloc, There's a reason . What happens when the heap becomes fragmented ? How developers handle memory allocation failures ? What about memory leaks ? These are difficult procedures , It takes code space and horsepower to handle , Traditional microcontrollers cannot handle .
Microcontrollers no longer “ Tradition ”.2015 year , The clock speed of a low-cost microcontroller may exceed 200 MHz, Flash memory space exceeded 1 MB,RAM the height is 64 KB( the height is 256 MB). Even in a less powerful system , It also has correct implementation and use malloc Tools for . The application may well guarantee this as well , Therefore, outdated best practices should not be excluded from consideration from the beginning just because of them .

features 3 – printf
Generally speaking , Embedded developers use in embedded systems C Library functions are considered bad practice . majority C Libraries are not reentrant , Usually bulky and slow to execute ( Or is it ?), Or it can be implemented by blocking execution until completion .printf The use of belongs to many of these categories , And has avoided using in embedded systems .
As mentioned earlier , Many changes have taken place between compiler optimization and hardware improvements , Use printf It's the guilt of the past . Considering the typical 32 kB Flash space , This function is considered to be “ Big ”, But it takes up very little code space . A typical implementation would printf Used as blocking function , This affects real-time response and consumes potential shared resources . By way of printf Link to circular buffer and interrupt driver transport driver , Response problems can be easily overcome , This allows program execution to continue , At the same time, the driver performs the necessary work .
features 4 – memset
Most involve memory operations or dynamic memory C Features are ultimately prohibited . The reason is obvious , Many embedded developers and teams have suffered in the past when using such features . When things like malloc or memset When functions like this make the most sense , Although they should not be avoided . contrary , Developers should ensure that they fully understand how to use this feature , What precautions are required for proper use , How to recover when the worst happens , Of course, there are appropriate tests to ensure that everything goes according to plan .
features 5 – C Bit fields
A concept is used in C Fuzzy features defined in the standard , The use of this concept is not rooted in the progress of compiler technology or hardware . A good example of this feature is the bit field . The biggest problem with using bit fields is that there are usually portability problems . A simple example is , Bit ordering is not standard , Compilers can usually rearrange these bits , Make it the most effective implementation . Adding padding bytes to the overall structure is also problematic .
The general rule of thumb is to avoid using bit fields , But there are many cases where they make sense , Even portability issues don't matter . A good example of using a bit field is to create a structure , This structure is used to create the configuration table of the initialization driver or application . Read the value of the bit by writing initialization , Reordering or even filling bytes is not a problem .
Conclusion
Standards and best practices are designed to help developers avoid throwing themselves in the foot , But they are just best practices . A standard might say that the use of C features , But the truth is , It is up to the developer to decide, in his own unique circumstances, whether traditional wisdom is applicable . Challenge our preconceptions , And make sure we understand why those best practices are appropriate , This is as important as following them . Don't forget about their applications 、 Give up risk and reward . Embedded developers are considering these “ prohibit ” The characteristics of , Need to know their time 、 Performance and size limitations .
边栏推荐
- 正则系列之断言Assertions
- Loss function: Diou loss handwriting implementation
- PG Basics - logical structure management (table inheritance, partition table)
- Motor control Clarke( α/β) Derivation of equal amplitude transformation
- Exploring the source code of Boca Cross Chain Communication: Elements
- VisualStudio and SQL
- STM32 porting the fish component of RT thread Standard Edition
- 幸运哈希竞猜系统开发(源码部署)趣投哈希游戏玩法开发(案例需求)
- 腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?
- 数据湖(十一):Iceberg表数据组织与查询
猜你喜欢

Loss function: Diou loss handwriting implementation

可觀測,才可靠:雲上自動化運維CloudOps系列沙龍 第一彈

【刷题篇】供暖器

Multi terminal collaboration of Huawei accounts to create a better internet life

数据湖(十一):Iceberg表数据组织与查询

资源变现小程序开通流量主教程

DeFi“钱从哪来”?一个大多数人都没搞清楚的问题

Rk356x u-boot Institute (command section) 3.2 usage of help command

There is no utf8 option for creating tables in Navicat database.

发生QQ大规模盗号事件,暴露出什么网络安全问题?
随机推荐
In line with the trend of media integration, Zhongke Wenge and Meishe jointly create digital intelligence media publicity
golang基础 —— 切片几种声明方式
Loss function: Diou loss handwriting implementation
【刷题篇】供暖器
WTM重大更新,多租户和单点登录
(8)JMeter元件详解之 Once only Controller 仅一次控制器
【刷题篇】爱吃香蕉的珂珂
Product manager professional knowledge 50 (7) - how to establish a complete set of user growth system?
[KALI] KALI系统、软件更新(附带镜像源)
How to take the first step in digital transformation
SQL编程问题,测试用例不通过
STM32 porting the fish component of RT thread Standard Edition
服务线上治理
JS converts an array to a two-dimensional array based on the same value
Unity Animator 参数
【刷题篇】避免洪水泛滥
资源变现小程序开通微信官方小商店教程
JS 二维数组变一维数组的方法
Motor control Clarke( α/β) Derivation of equal amplitude transformation
Observable, reliable: the first shot of cloudops series Salon of cloud automation operation and maintenance