确实的sizeof(T)==的sizeof(常量T)和alignof(T)== alignof(常量T) [英] Does sizeof(T) == sizeof(const T) and alignof(T) == alignof(const T)

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

问题描述

似乎可以合理地假设 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和闪存(这是只读)。 RAM为8位寻址,而Flash是只有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个字节长....但在双字节寻址闪存(这是一个常量变量将驻留)这结构将必须至少是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.

因此​​,这里是我的问题:

So here is my question:

执行C和C ++标准保证了常量和非常量类型的大小和对齐方式?

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

推荐答案

第3.9.3节:

一型的CV-合格或CV-不合格的版本是不同的
  类型;但是,应当有相同的再presentation和对齐
  要求(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-合格这里指的是常量挥发性。因此,答案是肯定的。

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

常量挥发性只能指定访问指定对象的局限性/属性。它们不被认为是基类型本身的一部分;因此,它们可以在不影响的类型的属性。

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(常量T)和alignof(T)== alignof(常量T)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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