简体   繁体   English

为什么C标准提供不定尺寸的类型(int,long long,char与int32_t,int64_t,uint8_t等)?

[英]Why does the C standard provide unsized types (int, long long, char vs. int32_t, int64_t, uint8_t etc.)?

Why weren't the contents of stdint.h the standard when it was included in the standard (no int no short no float, but int32_t, int16_t, float32_t etc.)? 为什么不是stdint.h 标准的内容,当它被包含在标准(无INT无短路无浮动,但int32_t和int16_t,float32_t等)? What advantage did/does ambiguous type sizes provide? 模棱两可的字体大小提供/提供了什么优势?

In objective-C, why was it decided that CGFloat , NSInteger , NSUInteger have different sizes on different platforms? 在Objective-C中,为什么决定在不同平台上CGFloatNSIntegerNSUInteger具有不同的大小?

When C was designed, there were computers with different word sizes. 在设计C时,有些计算机具有不同的字长。 Not just multiples of 8, but other sizes like the 18-bit word size on the PDP-7. 不仅是8的倍数,还包括其他大小,例如PDP-7上的18位字大小。 So sometimes an int was 16 bits, but maybe it was 18 bits, or 32 bits, or some other size entirely. 因此,有时一个int是16位,但也许是18位或32位,或完全是其他大小。 On a Cray-1 an int was 64 bits. 在Cray-1上, int为64位。 As a result, int meant "whatever is convenient for this computer, but at least 16 bits". 结果, int表示“对于此计算机而言方便的任何事物,但至少16位”。

That was about forty years ago. 那是大约四十年前。 Computers have changed, so it certainly looks odd now . 计算机已经改变,所以它当然现在看起来很奇怪。

NSInteger is used to denote the computer's word size, since it makes no sense to ask for the 5 billionth element of an array on a 32-bit system, but it makes perfect sense on a 64-bit system. NSInteger用于表示计算机的字长,因为在32位系统上要求数组的第50亿个元素没有意义,而在64位系统上则要求数组的十亿分之一。

I can't speak for why CGFloat is a double on 64-bit system. 我不能说为什么CGFloat是64位系统上的double That baffles me. 那使我感到困惑。

C is meant to be portable from enbedded devices, over your phone, to descktops, mainfraimes and beyond. C旨在从嵌入式设备,手机通过便携式设备移动到桌面,主机和其他方面。 These don't have the same base types, eg the later may have uint128_t where others don't. 它们没有相同的基本类型,例如,后者可能具有uint128_t ,而其他类型则没有。 Writing code with fixed width types would severely restrict portability in some cases. 在某些情况下,编写具有固定宽度类型的代码将严重限制可移植性。

This is why with preference you should neither use uintX_t nor int , long etc but the semantic typedefs such as size_t and ptrdiff_t . 这就是为什么偏爱你应该既不用uintX_t也不intlong等,但语义的typedef如size_tptrdiff_t These are really the ones that make your code portable. 这些确实是使您的代码可移植的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM