如何满足对齐要求?

发布于 2025-01-10 06:19:22 字数 1816 浏览 0 评论 0 原文

我认为我误读了标准引文,因此我不完全理解措辞的确切意图是什么。

首先,我已经知道对齐要求是什么,但是我无法弄清楚对齐要求和一般铸造之间的确切关系,以及当我执行 static_casting 或重新解释_铸造。我想现在读者已经明白了我的第一个问题。

其次,标准引文中有一些词我花了两天时间才看懂,但还是不明白。来自以下段落:

N4885: 7.6.1.9 静态转换 [expr.static.cast]

“指向 cv1 void 的指针”类型的纯右值可以转换为纯右值 类型为“指向 cv2 T 的指针”,其中 T 是对象类型,cv2 是 与 cv1 相同或更高的 cv 资格。 如果 原始指针值代表一个字节的地址A 内存且A不满足T的对齐要求,则 结果指针值未指定

在这里,他们说“如果原始指针值不满足 T 的对齐要求,则结果指针值未指定”。这到底意味着什么?

我不明白的是这个“原始指针值”什么时候满足T的对齐要求,什么时候不满足,以避免这种未指定的指针值。我只需要有人用简单的例子来解释上面引文中的粗体部分;作为一名程序员,我必须从那个大胆的部分中了解什么以及我必须避免什么。例如:

int i = 12;
double *pd = static_cast<double *>(static_cast<void *>(&i)); // does 'pd' has unspecified address value?.
short *ps = static_cast<short *>(static_cast<void *>(&i));   // does 'ps' has unspecified address value?.

最后,我需要理解一个相对相同的句子:

N4885: 7.6.1.10 Reinterpret cast [expr.reinterpret.cast]

对象指针可以显式转换为对象指针 不同的类型。61 当对象指针类型的纯右值 v 为 转换为对象指针类型“pointer to cv T”,结果为 static_cast(static_cast(v))。 [注7:转换a “指向 T1 的指针”类型的指针,指向 T1 类型的对象 类型“指向 T2 的指针”(其中 T2 是对象类型, T2 的对齐要求并不比 T1 更严格) 返回其原始类型会产生原始指针值。 - 结尾 注]。

标准中这句话“T2的对齐要求并不比T1更严格”是什么意思,“比T1更严格”是什么意思。

我想如果我有这个 static_assert 表达式,那么 T2 的对齐要求可能不会比 T1 更严格: static_assert(alignof(T1) >=alignof(T2));或者这个断言在某些情况下并不成立。

int i = 34;
double *pd = reinterpret_cast<double *>(&i); // does 'pd' has unspecified address value?.
short *ps = reinterpret_cast<short *>(&i);   // does 'ps' has unspecified address value?.

我添加这些示例只是为了弄清楚我的问题所在,而不仅仅是回答 // comments 中的问题

I think that I am misreading the standard quotation, hence I do not fully understand what's the exact intent of the wording.

Firstly, I am already aware of what alignment requirement is, but I can't figure out the exact relation between alignment requirement and casting in general, and what're the points I should care about, regarding alignment requirement, when I perform static_casting or reinterpret_casting. I think now the reader got my first question.

Secondly, there're some words in the standard quotation I spend two days to understand them but I don't. From the paragraph in:

N4885: 7.6.1.9 Static cast [expr.static.cast]

A prvalue of type “pointer to cv1 void” can be converted to a prvalue
of type “pointer to cv2 T”, where T is an object type and cv2 is the
same cv-qualification as, or greater cv-qualification than, cv1. If
the original pointer value represents the address A of a byte in
memory and A does not satisfy the alignment requirement of T, then the
resulting pointer value is unspecified
.

Here, they said "if the original pointer value doesn't satisfy the alignment requirement of T, then the resulting pointer value is unspecified". What really does that mean?

What's I can't understand is when does this "original pointer value" satisfies the alignment requirement of T, and when does not, to avoid such unspecified pointer value. I just need someone to explain the bold part from the above quote with simple examples; what I have to know, as a programmer, from that bold part and what I have to avoid. For example:

int i = 12;
double *pd = static_cast<double *>(static_cast<void *>(&i)); // does 'pd' has unspecified address value?.
short *ps = static_cast<short *>(static_cast<void *>(&i));   // does 'ps' has unspecified address value?.

Finally, there's a relatively same sentence I need to understand in:

N4885: 7.6.1.10 Reinterpret cast [expr.reinterpret.cast]

An object pointer can be explicitly converted to an object pointer of
a different type.61 When a prvalue v of object pointer type is
converted to the object pointer type “pointer to cv T”, the result is
static_cast<cv T*>(static_cast<cv void*>(v)). [Note 7: Converting a
pointer of type “pointer to T1” that points to an object of type T1 to
the type “pointer to T2” (where T2 is an object type and the
alignment requirements of T2 are no stricter than those of T1)
and
back to its original type yields the original pointer value. — end
note].

What does the standard mean by this sentence "the alignment requirements of T2 are no stricter than those of T1", what does the word "stricter than" mean.

I think if I have this static_assert expression, then maybe the alignment requirements of T2 would not be stricter than those of T1: static_assert(alignof(T1) >= alignof(T2)); Or this assertion is not true for some cases.

int i = 34;
double *pd = reinterpret_cast<double *>(&i); // does 'pd' has unspecified address value?.
short *ps = reinterpret_cast<short *>(&i);   // does 'ps' has unspecified address value?.

I am added these example to just clear what my problem lies, not to just answer the questions in the // comments

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

本王不退位尔等都是臣 2025-01-17 06:19:22

虽然常见的实现具有类整数指针(例如,reinterpret_cast 的行为类似于指针和整数之间的 memcpy,并且指针上的算术反映在整数中)值),该标准像往常一样仅提供弱保证来支持不太常见的体系结构,其中指针具有其他格式和/或特殊寄存器。因此,不可能观察动态指针值的对齐情况:如果 alignof(expression_type),则应用未指定的值,除非指针引用使用 alignas实际类型更强对齐的对象。

这意味着,对于存在 reinterpret_cast 的“临时指针存储”来说,double* 是一个很差的类型;幸运的是,大多数 C++ 代码使用模板(因此不会出现这种情况),旧的 C 代码使用 char* (其对齐方式为 1),其他代码使用 void* (没有对齐),所以这里很少有实际问题。

While common implementations have integer-like pointers (such that reinterpret_cast behaves like memcpy between pointers and integers and arithmetic on the pointers is reflected in the integer values), the standard as usual provides only weak guarantees to support less common architectures where pointers have other formats and/or special registers. As such, it’s impossible to observe alignment of a dynamic pointer value: the unspecified value applies if alignof(expression_type)<alignof(cast_type) unless the pointer refers to an object declared with alignas or to an object whose actual type is more strongly aligned.

This means that double* is a poor type to use for the sort of “temporary pointer storage” for which reinterpret_cast exists; fortunately, most C++ code uses templates (so this doesn’t come up), old C code uses char* (whose alignment is 1), and other code uses void* (which has no alignment), so there’s rarely an actual issue here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文