首页 > 编程语言 >C++学习------cstdint头文件的源码学习01---类型定义

C++学习------cstdint头文件的源码学习01---类型定义

时间:2022-11-16 22:37:55浏览次数:74  
标签:__ typedef 01 int 32 long --- 源码 uint

引言

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;

后续宏定义讲解见下篇

标签:__,typedef,01,int,32,long,---,源码,uint
From: https://blog.51cto.com/u_15830688/5857812

相关文章

  • Netty源码-05-EventLoop
    前文已经了解过了NioEventLoopGroup和NioEventLoop在Netty中是用的是Reactor线程模型(IO多路复用器+多个线程),真正处理业务流程的worker线程都是单个线程,一个线程处理多个......
  • Netty源码-06-MpscQueue
    在IO线程NioEventLoop中维护了一个队列实现,用于存放非IO任务,一个IO线程负责N个Channel,为了保证一个线程贯穿始终负责一个Channel的所有任务(任务执行次序有先后区分需要),因......
  • 循环练习-判断回文数
    Scanners=newScanner(System.in);//引用键盘录入功能System.out.println("本程序用于判断回文数,请输入您要判断的值:");intx=s.nextInt();//将键盘输入的......
  • Javascript(笔记40) - ES6特性 - Map
    Javascript(笔记40)-ES6特性-MapMap ES6 提供了Map数据结构。它类似于对象,也是键值对集合。但是“键”的范围不限于字符串,各种类型的值(包括对象)都可以当作键。Map也实......
  • Netty源码-07-Channel
    一类图关系在Java的NIO体系中定义了ServerSocketChannel和SocketChannelNetty为了支持Reactor线程模型和异步编程,自己也实现了与Java中对应的两个实现NioServerSocke......
  • Netty源码-09-ServerBootstrapAcceptor
    在ServerBootstrapAcceptor启用之前,此刻Reactor状态应该是NioServerSocketChannel在IO多路复用器上关注着Accept(16)事件pipeline中有4个handlerheadbossHandlerSer......
  • Netty源码-08-ChannelInitializer
    一回顾几个时机点pipeline的初始化用户向pipeline添加ChannelInitializer辅助实例Channel注册到复用器之后回调1pipeline的初始化初始化Channel的时候触发了pipel......
  • Netty源码-10-ChannelFuture
    Netty为了提高系统的吞吐,大量使用异步线程模型一DemopublicclassFutureTest00{publicstaticvoidmain(String[]args)throwsInterruptedException,Execut......
  • 128-hql 转 sql
    Stringtest(Stringhql){ QueryTranslatorImpltranslator=newQueryTranslatorImpl("queryIdentifier",hql, Collections.EMPTY_MAP,(SessionFactoryImplemento......
  • 【221116-3】已知12的a次方=18,求2的(2a-1)/(a-2)次方。
    ......