sizeof(T)== sizeof(const T)和alignof(T)== alignof(const T) [英] Does sizeof(T) == sizeof(const T) and alignof(T) == alignof(const T)

查看:277
本文介绍了sizeof(T)== sizeof(const T)和alignof(T)== alignof(const T)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎合理的假设 T const T 将是两种类型的大小相同,

It seems reasonable to assume that T and const T would be two types that would be the same size and have the same alignment, but after thinking about some real systems, it seems that they could be different.

让我解释一下:

假设你有一个有两种类型内存的系统:RAM和Flash(这是只读的)。 RAM是8位可寻址的,而闪存只有16位可寻址。假设这是 T

Suppose you have a system with two types of memory: RAM and Flash (which is read only). The RAM is 8 bit addressable, while the Flash is only 16 bit addressable. Suppose this is T:

struct T
{
  uint8_t x;
  uint16_t y;
};

在字节可寻址RAM中,这个结构将是3字节长....但是在双字节可寻址的闪存(这是一个 const 变量将驻留)这个结构将不得不至少4个字节长,因为对齐问题。

In the byte addressable RAM this struct would be 3 bytes long.... but in the double byte addressable Flash (which is where a const variable would reside) this struct would have to be at least 4 bytes long, because of alignment issues.

这里是我的问题:

c和c ++标准保证 const 和非 const 类型?

Do the c and c++ standards guarantee the sizes and alignment of const and nonconst types?

推荐答案

3:


类型的cv限定或cv非限定版本是不同的
类型;然而,它们应具有相同的代表和一致性
要求(3.11)。 53

The cv-qualified or cv-unqualified versions of a type are distinct types; however, they shall have the same representation and alignment requirements (3.11). 53

cv-qualified这里指 const volatile 。所以答案是,是的。

"cv-qualified" here refers to const and volatile. So the answer is, yes.

const volatile 仅指定对指定对象的访问的限制/属性。它们不被认为是基本类型本身的一部分;因此它们不会影响类型的属性。

const and volatile only specify the limitations/attributes of access to the specified object. They are not considered to be a part of the base type itself; hence they cannot affect the type's properties.

这篇关于sizeof(T)== sizeof(const T)和alignof(T)== alignof(const T)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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