我可以保证sizeof(type)== sizeof(unsigned type)吗? [英] Do I have the guarantee that sizeof(type) == sizeof(unsigned type)?

查看:139
本文介绍了我可以保证sizeof(type)== sizeof(unsigned type)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

char,int,long double的大小...从一个编译器到另一个编译器,可能有所不同.但是,根据C ++ 11或C11标准,我是否可以保证任何有符号和无符号基本整数类型的大小都相同?

The sizeof char, int, long double... can vary from one compiler to another. But do I have the guarantee according to the C++11 or C11 standard that the size of any signed and unsigned fundamental integral type is the same ?

推荐答案

C ++ 11标准对整数类型进行了说明:

The C++11 Standard says about integer types:

(§3.9.1/3)对于每种标准有符号整数类型,都有一个对应的(但不同的)标准无符号整数类型:"unsigned char","unsigned short int","unsigned int","unsigned long int"和"unsigned long long int",它们各自与相同的有符号整数类型占用相同的存储量,并具有相同的对齐要求(3.11);也就是说,每个有符号整数类型与其对应的无符号整数类型具有相同的对象表示形式.同样,对于每种扩展的带符号整数类型,都存在一个对应的扩展的无符号整数类型,具有相同数量的存储和对齐要求. [...]

(§3.9.1/3) For each of the standard signed integer types, there exists a corresponding (but different) standard unsigned integer type: "unsigned char", "unsigned short int", "unsigned int", "unsigned long int", and "unsigned long long int", each of which occupies the same amount of storage and has the same alignment requirements (3.11) as the corresponding signed integer type; that is, each signed integer type has the same object representation as its corresponding unsigned integer type. Likewise, for each of the extended signed integer types there exists a corresponding extended unsigned integer type with the same amount of storage and alignment requirements. [...]

所以答案是肯定的.

对于C11,此处有一个对应的语句(来自N1570):

For C11, there is a corresponding statement here (this is from N1570):

(§6.2.5/6)对于每个有符号整数类型,都有一个对应的(但不同的)无符号整数 使用相同数量的整数类型(用关键字unsigned指定) 存储(包括标志信息),并且具有相同的对齐要求[...]

(§6.2.5/6) For each of the signed integer types, there is a corresponding (but different) unsigned integer type (designated with the keyword unsigned) that uses the same amount of storage (including sign information) and has the same alignment requirements [...]

这篇关于我可以保证sizeof(type)== sizeof(unsigned type)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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