2017-03-02 42 views
0

例如,在我的实现中,将char定义为1个字节,那么它是否只是在GCC中实现的精确宽度整数类型如何?

typedef int8_t char;

在stdint.h中,还是有实际的内存操作正在进行?

+1

如果系统不支持它们,则不保证具有完整宽度的整数类型。改用* int_leastN_t *版本。这就是说实现最有可能使用条件编译来决定每个支持系统的每种typedef。 – DeiDei

+0

这是什么不支持他们?它是OS还是编译器还是芯片?我的个人项目是对整个c标准库进行重新编码,所以我猜这取决于我选择了吗? –

+0

该标准要求它们是typedef(如果它们存在,那就是) –

回答

2

系统stdint.h可以对体系结构进行假设,并知道诸如short或int的大小。在macOS上的/usr/include/stdint.h非常容易阅读。这是相关的位。

/* 7.18.1.1 Exact-width integer types */ 
#include <sys/_types/_int8_t.h> 
#include <sys/_types/_int16_t.h> 
#include <sys/_types/_int32_t.h> 
#include <sys/_types/_int64_t.h> 

#include <_types/_uint8_t.h> 
#include <_types/_uint16_t.h> 
#include <_types/_uint32_t.h> 
#include <_types/_uint64_t.h> 

/* 7.18.1.2 Minimum-width integer types */ 
typedef int8_t   int_least8_t; 
typedef int16_t   int_least16_t; 
typedef int32_t   int_least32_t; 
typedef int64_t   int_least64_t; 
typedef uint8_t   uint_least8_t; 
typedef uint16_t  uint_least16_t; 
typedef uint32_t  uint_least32_t; 
typedef uint64_t  uint_least64_t; 

您可以挖掘每个单独的头文件并查看typedef。通过他们每个人看,他们非常简单。

uint*_t只是未签名的版本。


由于GCC不是Mac上的系统编译器,这是铛,它不能对架构的假设,因此它使用现有的stdint.h。这里是gcc的stdint.h。它加载系统stdint.h,加上自己的头文件来添加自己的非标准事物。

$ cat /opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.3.0/include/stdint.h 
#ifndef _GCC_WRAP_STDINT_H 
#if __STDC_HOSTED__ 
# if defined __cplusplus && __cplusplus >= 201103L 
# undef __STDC_LIMIT_MACROS 
# define __STDC_LIMIT_MACROS 
# undef __STDC_CONSTANT_MACROS 
# define __STDC_CONSTANT_MACROS 
# endif 
# include_next <stdint.h> 
#else 
# include "stdint-gcc.h" 
#endif 
#define _GCC_WRAP_STDINT_H 
#endif 
相关问题