嵌入式 C++:是否使用 STL?

2022-01-25 00:00:00 embedded c++ stl

我一直是嵌入式软件工程师,但通常在 OSI 堆栈的第 3 层或第 2 层.我不是一个真正的硬件人.我一般一直做电信产品,通常是手机/手机,这通常意味着类似 ARM 7 处理器的东西.

I have always been an embedded software engineer, but usually at Layer 3 or 2 of the OSI stack. I am not really a hardware guy. I have generally always done telecoms products, usually hand/cell-phones, which generally means something like an ARM 7 processor.

现在我发现自己处于一个更通用的嵌入式世界,在一家小型初创公司中,我可能会转向不那么强大"的处理器(这是主观的一点)――我无法预测是哪个.

Now I find myself in a more generic embedded world, in a small start-up, where I might move to "not so powerful" processors (there's the subjective bit) - I cannot predict which.

我已经阅读了很多关于在嵌入式系统中使用 C++ 中的 STL 的争论,但没有明确的答案.关于可移植性有一些小小的担忧,还有一些关于代码大小或运行时的担忧,但我有两个主要担忧:
1 - 异常处理;我仍然不确定是否使用它(参见 Embedded C++ : to use exceptions or不是吗?)
2 - 我非常不喜欢嵌入式系统中的动态内存分配,因为它会带来一些问题.我通常有一个在编译时静态分配的缓冲池,它只提供固定大小的缓冲区(如果没有缓冲区,系统重置).当然,STL 做了很多动态分配.

I have read quite a bit about debate about using STL in C++ in embedded systems and there is no clear cut answer. There are some small worries about portability, and a few about code size or run-time, but I have two major concerns:
1 - exception handling; I am still not sure whether to use it (see Embedded C++ : to use exceptions or not?)
2 - I strongly dislike dynamic memory allocation in embedded systems, because of the problems it can introduce. I generally have a buffer pool which is statically allocated at compile time and which serves up only fixed size buffers (if no buffers, system reset). The STL, of course, does a lot of dynamic allocation.

现在我必须决定是使用还是放弃 STL - 为整个公司,永远(它将进入一些非常核心的软件).

Now I have to make the decision whether to use or forego the STL - for the whole company, for ever (it's going into some very core s/w).

我该往哪边跳?超级安全&失去了构成 C++ 的大部分内容(imo,它不仅仅是语言定义),以后可能会遇到问题,或者必须添加大量异常处理 &现在也许还有其他代码?

Which way do I jump? Super-safe & lose much of what constitutes C++ (imo, it's more than just the language definition) and maybe run into problems later or have to add lots of exception handling & maybe some other code now?

我很想只使用 Boost,但是 1) 我不确定它是否会移植到我可能想要使用的每个嵌入式处理器和 2)在他们的网站上,他们说他们不保证/不推荐嵌入式系统的某些部分(尤其是 FSM,这看起来很奇怪).如果我选择 Boost &稍后我们发现问题....

I am tempted to just go with Boost, but 1) I am not sure if it will port to every embedded processor I might want to use and 2) on their website, they say that they doesn't guarantee/recommend certain parts of it for embedded systems (especially FSMs, which seems weird). If I go for Boost & we find a problem later ....

推荐答案

超级安全&失去很多东西构成C++(imo,它不仅仅是只是语言定义)和以后可能会遇到问题或有添加大量异常处理&现在也许还有其他代码?

Super-safe & lose much of what constitutes C++ (imo, it's more than just the language definition) and maybe run into problems later or have to add lots of exception handling & maybe some other code now?

我们在游戏世界中也有类似的争论,人们站在双方的立场上.关于引用的部分,您为什么要担心丢失构成 C++ 的大部分内容"?如果它不实用,请不要使用它.是否是C++"并不重要.

We have a similar debate in the game world and people come down on both sides. Regarding the quoted part, why would you be concerned about losing "much of what constitutes C++"? If it's not pragmatic, don't use it. It shouldn't matter if it's "C++" or not.

运行一些测试.你能以令你满意的方式绕过 STL 的内存管理吗?如果是这样,是否值得付出努力?STL 和 boost 旨在解决很多问题,如果您的设计旨在避免随意的动态内存分配,请不要提出... STL 是否解决了您面临的特定问题?

Run some tests. Can you get around STL's memory management in ways that satisfy you? If so, was it worth the effort? A lot of problems STL and boost are designed to solve just plain don't come up if you design to avoid haphazard dynamic memory allocation... does STL solve a specific problem you face?

很多人都在紧张的环境中处理过 STL,并且对此感到满意.很多人只是避而远之.有人提出全新标准.我认为没有一个正确的答案.

Lots of people have tackled STL in tight environments and been happy with it. Lots of people just avoid it. Some people propose entirely new standards. I don't think there's one right answer.

相关文章