C++学习------cstdint头文件的源码学习01

510 阅读3分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第16天,点击查看活动详情

引言

cstdint头文件是C++对stdint头文件的封装,这个头文件定义了一系列特定长度的类型别名,一系列值的上下限,以及一系列类型转换的宏。我们一起来看看它的内部实现。 代码参考: www.aospxref.com/android-12.…

stdint.h的源码实现

类型别名定义

常见的基础整型有char、short、int、long、long long,再加上unsigned和signed的修饰,就有多种不同的组合,特别是在32位机器和64位机器上各种数据类型所占用的位数有不同。

C语言中只规定了基本的约束:short和int型至少为16位,long型至少为32位,并且short型长度不能超过int型,而int型不能超过long型。

数据类型占内存的位数实际上与操作系统的位数和编译器(不同编译器支持的位数可能有所不同)都有关 ,具体某种数据类型占字节数得编译器根据操作系统位数两者之间进行协调好后分配内存大小。

stdint.h文件中就考虑到了这一点,定义了许多与机器位数无关的类型,一定是固定的位数。

内部类型:__intx_t、__uintx_t(x=8,16,32,64),__intptr_t、__uintptr_t

36  typedef signed char __int8_t;
37  typedef unsigned char __uint8_t;
38  typedef short __int16_t;
39  typedef unsigned short __uint16_t;
40  typedef int __int32_t;
41  typedef unsigned int __uint32_t;
42  #if defined(__LP64__)
43  typedef long __int64_t;
44  typedef unsigned long __uint64_t;
45  #else
46  typedef long long __int64_t;
47  typedef unsigned long long __uint64_t;
48  #endif
49  
50  #if defined(__LP64__)
51  typedef long __intptr_t;
52  typedef unsigned long __uintptr_t;
53  #else
54  typedef int __intptr_t;
55  typedef unsigned int __uintptr_t;
56  #endif

定义内部类型,说明一下__LP64__是64位机器会定义的一个宏,可以用它来判断机器类型,通过上面的别名设置,我们也可以推测出,在32位机器上,long long 占用64位,int和long占用32位,short占用16位,char占用8位。

基础类型定义

这里是对上面内部类型的封装,C语言中一般双下划线开头的都是内部类型,不建议直接在代码中使用。 8/16/32/64/ptr的封装没有什么变化,这里还额外定义了

  • uint_leastx_t/int_leastx_t(x=8,16,32,64),该类型说明不存在占位大小较小且至少具有指定宽度的其他整数类型
  • uint_fastx_t/int_fastx_t(x=8,16,32,64),该类型至少与具有指定宽度的任何其他整数类型一样快。
  • uintmax_t/intmax_t,占位最大的size

即这样的定义完全与机器位数无关,给定的类型定义一定拥有准确的位数,不会出现迁移的异常,正常的使用过程中也建议使用这样的类型定义,避免在32位机器与64位机器上表现不一致。

58  typedef __int8_t      int8_t;
59  typedef __uint8_t     uint8_t;
60  
61  typedef __int16_t     int16_t;
62  typedef __uint16_t    uint16_t;
63  
64  typedef __int32_t     int32_t;
65  typedef __uint32_t    uint32_t;
66  
67  typedef __int64_t     int64_t;
68  typedef __uint64_t    uint64_t;
69  
70  typedef __intptr_t    intptr_t;
71  typedef __uintptr_t   uintptr_t;
72  
73  typedef int8_t        int_least8_t;
74  typedef uint8_t       uint_least8_t;
75  
76  typedef int16_t       int_least16_t;
77  typedef uint16_t      uint_least16_t;
78  
79  typedef int32_t       int_least32_t;
80  typedef uint32_t      uint_least32_t;
81  
82  typedef int64_t       int_least64_t;
83  typedef uint64_t      uint_least64_t;
84  
85  typedef int8_t        int_fast8_t;
86  typedef uint8_t       uint_fast8_t;
87  
88  typedef int64_t       int_fast64_t;
89  typedef uint64_t      uint_fast64_t;
90  
91  #if defined(__LP64__)
92  typedef int64_t       int_fast16_t;
93  typedef uint64_t      uint_fast16_t;
94  typedef int64_t       int_fast32_t;
95  typedef uint64_t      uint_fast32_t;
96  #else
97  typedef int32_t       int_fast16_t;
98  typedef uint32_t      uint_fast16_t;
99  typedef int32_t       int_fast32_t;
100  typedef uint32_t      uint_fast32_t;
101  #endif
102  
103  typedef uint64_t      uintmax_t;
104  typedef int64_t       intmax_t;

后续宏定义讲解见下篇