且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

哪些部分的C是最便携?

更新时间:2023-11-06 09:42:58

在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.