为什么C提供的整数类型对于任何项目都不够好? [英] Why aren't the C-supplied integer types good enough for basically any project?

查看:137
本文介绍了为什么C提供的整数类型对于任何项目都不够好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更像是一个系统管理员而不是一个程序员。但是我花了大量的时间在程序员的代码中徘徊,试图弄清楚出了什么问题。当程序员期望__u_ll_int32_t的一个定义或者其他什么(是的,我知道那不是真的)时,会有一些令人不安的 时间用于处理问题,但要么预期定义该类型的文件在某处除了它之外,或者(这是更糟糕的,但幸运的是,很少见)预期该定义的语义不是它的。

I'm much more of a sysadmin than a programmer. But I do spend an inordinate amount of time grovelling through programmers' code trying to figure out what went wrong. And a disturbing amount of that time is spent dealing with problems when the programmer expected one definition of __u_ll_int32_t or whatever (yes, I know that's not real), but either expected the file defining that type to be somewhere other than it is, or (and this is far worse but thankfully rare) expected the semantics of that definition to be something other than it is.

据我了解C,它故意不对整数类型进行宽度定义(并且这是一件好事),而是给程序员 char short int long long long ,在他们所有的签名和未签名的荣耀中,具有定义的最小值,实现(希望)满足。此外,它为程序员提供了各种宏,实现必须提供这些宏来告诉你诸如char的宽度,最大的unsigned long等等。然而,任何非平凡的C项目似乎要做的第一件事就是导入或发明另一组类型,它们明确地给出了8位,16位,32位和64位整数。这意味着作为系统管理员,我必须将这些定义文件放在程序员期望的地方(毕竟,我的工作),但不是所有这些定义的所有语义都是相同的(这个轮已经多次重新发明)并且我不知道在这里满足所有用户需求的非临时方式。 (我有时会使用< bits / types_for_ralph.h>,我知道每次我都会让小狗哭。)

As I understand C, it deliberately doesn't make width definitions for integer types (and that this is a Good Thing), but instead gives the programmer char, short, int, long, and long long, in all their signed and unsigned glory, with defined minima which the implementation (hopefully) meets. Furthermore, it gives the programmer various macros that the implementation must provide to tell you things like the width of a char, the largest unsigned long, etc. And yet the first thing any non-trivial C project seems to do is either import or invent another set of types that give them explicitly 8, 16, 32, and 64 bit integers. This means that as the sysadmin, I have to have those definition files in a place the programmer expects (that is, after all, my job), but then not all of the semantics of all those definitions are the same (this wheel has been re-invented many times) and there's no non-ad-hoc way that I know of to satisfy all of my users' needs here. (I've resorted at times to making a <bits/types_for_ralph.h>, which I know makes puppies cry every time I do it.)

尝试什么明确地定义数字的位宽(用一种特别不想做的语言)获得程序员,这使得所有这些配置管理头疼吗?为什么不知道定义的最小值和平台提供的MAX / MIN宏足以完成C程序员想要做的事情?你为什么要选择一种语言,它的主要优点是它可以在任意比特的平台上移植,然后自己输入特定的比特宽度?

What does trying to define the bit-width of numbers explicitly (in a language that specifically doesn't want to do that) gain the programmer that makes it worth all this configuration management headache? Why isn't knowing the defined minima and the platform-provided MAX/MIN macros enough to do what C programmers want to do? Why would you want to take a language whose main virtue is that it's portable across arbitrarily-bitted platforms and then typedef yourself into specific bit widths?

推荐答案

当C或C ++程序员(以下简称第二人称)选择整数变量的大小时,通常会出现以下情况之一:

When a C or C++ programmer (hereinafter addressed in second-person) is choosing the size of an integer variable, it's usually in one of the following circumstances:

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