哪些部分的C是最便携? [英] What parts of C are most portable?

查看:116
本文介绍了哪些部分的C是最便携?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我读和Lua共同创造者路易斯·H.德菲格雷多和罗伯托Ierusalimschy,他们讨论的设计,和Lua的实施面试。这是非常有趣的,至少可以说。然而,讨论的一部分在我的脑海带来的东西了。罗伯托谈到的Lua为独立的应用程序的(即,它是使用从OS没有什么纯粹的ANSI C)。他说,那Lua中的核心是完全可移植的,因为它的纯度的已经/能够被更容易和平台,甚至从来没有考虑移植(如机器人和嵌入式设备)。

I recently read an interview with Lua co-creators Luiz H. de Figueredo and Roberto Ierusalimschy, where they discussed the design, and implementation of Lua. It was very intriguing to say the least. However, one part of the discussion brought something up in my mind. Roberto spoke of Lua as a "freestanding application" (that is, it's pure ANSI C that uses nothing from the OS.) He said, that the core of Lua was completely portable, and because of its purity has been able to be ported much more easily and to platforms never even considered (such as robots, and embedded devices.)

现在这令我惊叹。 ℃的一般是非常便携的语言。那么,哪些部分C(即那些在标准库)是最不可移植?并且是指那些可以预期在大多数平台上的工作是什么?如果只有一套有限的数据类型的使用(例如避免也许浮动)?怎么样的文件 STDIO 系统? 的malloc 免费?看来,Lua中避免所有这些。是拿东西到了极致?或者它们的便携性问题的根源?除此之外,可以做什么其他的事情,使code便携至极?

Now this makes me wonder. C in general is a very portable language. So, what parts of C (namely those in the the standard library) are the most unportable? and what are those that can be expected to work on most platforms? Should only a limited set of data types be used (e.g. avoiding short and maybe float)? What about the FILE and the stdio system? malloc and free? It seems that Lua avoids all of these. Is that taking things to the extreme? Or are they the root of portability issues? Beyond this, what other things can be done to make code extremely portable?

我要求所有这一切的原因,是因为我目前正在写纯C89的应用程序,它是最佳的,它尽可能地轻便。我愿意采取实现它的中间道路(够便携,但没有这么多,我不得不写一切从头开始。)反正,我只是想看看有什么总体的关键是写最好的C $ C $Ç

The reason I'm asking all of this, is because I'm currently writing an application in pure C89, and it's optimal that it be as portable as possible. I'm willing take a middle road in implementing it (portable enough, but no so much that I have to write everything from scratch.) Anyways, I just wanted to see what in general is key to writing the best C code.

最后一点,所有这些讨论涉及到仅C89。

As a final note, all of this discussion is related to C89 only.

推荐答案

在Lua中的情况下,我们没有太多的抱怨C语言本身,而是我们发现,C标准库包含似乎很多功能无害,直接的使用,直到你认为他们不检查其输入的有效性(即如果inconveninent罚款)。 C标准说,处理不好的输入是不确定的行为,使这些职能为所欲为,甚至崩溃主机程序。考虑,例如,的strftime。有些libc中的根本忽略无效的格式说明,但其他的libc的(例如,在Windows中)崩溃!现在,的strftime不是关键的功能。为什么崩溃,而不是做一些合理的?因此,lua有调用strftime和出口的strftime到Lua程序变成一件苦差事前做自己的输入进行验证。因此,我们试图通过针对独立为核心,以保持从Lua内核这些问题的明确。但Lua的标准库不能做到这一点,因为他们的目标是设施出口到Lua程序,包括什么是C标准库。

In the case of Lua, we don't have much to complain about the C language itself but we have found that the C standard library contains many functions that seem harmless and straight-forward to use, until you consider that they do not check their input for validity (which is fine if inconveninent). The C standard says that handling bad input is undefined behavior, allowing those functions to do whatever they want, even crash the host program. Consider, for instance, strftime. Some libc's simply ignore invalid format specifiers but other libc's (e.g., in Windows) crash! Now, strftime is not a crucial function. Why crash instead of doing something sensible? So, Lua has to do its own validation of input before calling strftime and exporting strftime to Lua programs becomes a chore. Hence, we have tried to stay clear from these problems in the Lua core by aiming at freestanding for the core. But the Lua standard libraries cannot do that, because their goal is to export facilities to Lua programs, including what is available in the C standard library.

这篇关于哪些部分的C是最便携?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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