指针对齐 [英] pointer alignment

查看:68
本文介绍了指针对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于任何指向T的指针,指向T的指针是否与指向T的指针有不同或者可能有不同的对齐要求?如果是这样的话,

标准中的确切措辞在哪里呢?

解决方案

j0mbolar写道:


对于任何指向T的指针,指向T的指针是否与指向T的指针有不同或者可能有不同的对齐要求?如果是这样的话,
标准中的确切措辞在哪里可以建议呢?




我不能引用章节和经文,但我可以告诉你在平台上

我目前使用:


指向char的指针没有对齐限制。

指向16位值的指针必须在16位边界上对齐。

(即:地址必须是偶数。)

指针是32位,指针指针必须在

a 32位边界上对齐。 (即:地址必须是4的倍数。)


-

+ -------------- ----------- + -------------------- + ----------------- ------------ +

| Kenneth J. Brody | www.hvcomputer.com | |

| kenbrody / at\spamcop.net | www.fptech.com | #include< std_disclaimer.h> |

+ ------------------------- + -------------- ------ + ----------------------------- +


< BLOCKQUOTE>" j0mbolar" < J0 ****** @ engineer.com>在消息中写道

新闻:2d ************************** @ posting.google.c om ...

对于任何指向T的指针,指向T的指针是否与指向T的指针有不同或者可能有不同的对齐要求?如果是这样的话,
标准中的确切措辞在哪里?




6.2.5#26:


指向void的指针具有相同的表示和对齐方式

要求作为指向字符类型的指针。同样,指向

合格或不合格版本的兼容类型的指针应具有相同的

表示和对齐要求。所有指向结构类型的指针

应具有相同的表示和对齐要求。

所有指向联合类型的指针应具有相同的表示形式和

对齐要求彼此。指向其他类型的指针不需要具有相同的表示或对齐要求。


Kenneth Brody写道:

< br0> j0mbolar写道:


对于任何指向T的指针,指向T的指针是否有不同的指针要求或者指针指向T的指针?如果是这样的话,
标准中的确切措辞在哪里呢?
我不能引用章节和经文,但我可以告诉你,在平台上
我目前正在使用:

指向char的指针没有对齐限制。




char没有对齐限制。指向char的指针可以。

指向16位值的指针必须在16位边界上对齐。
(即:地址必须是偶数。)
指针是32位,指针指针必须在32位边界上对齐。 (即:地址必须是4的倍数。)




这也不是必需的。事实上,基于PC的计算机不需要

(至少有些编译器没有)对于

指针和16位整数的对齐要求。


Thad


for any pointer to T, does a pointer to T have different or can have
different alignment requirement than a pointer to pointer to T? if so,
where is the exact wording in the standard that would suggest so?

解决方案

j0mbolar wrote:


for any pointer to T, does a pointer to T have different or can have
different alignment requirement than a pointer to pointer to T? if so,
where is the exact wording in the standard that would suggest so?



I can''t quote chapter and verse, but I can tell you that on the platforms
I currently use:

A pointer to "char" has no alignment restrictions.
A pointer to a 16-bit value must be aligned on a 16-bit boundary.
(ie: the address must be even.)
Pointers are 32 bits, and a pointer-to-pointer must be aligned on
a 32-bit boundary. (ie: the address must be a multiple of 4.)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+


"j0mbolar" <j0******@engineer.com> wrote in message
news:2d**************************@posting.google.c om...

for any pointer to T, does a pointer to T have different or can have
different alignment requirement than a pointer to pointer to T? if so,
where is the exact wording in the standard that would suggest so?



6.2.5#26:

A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type. Similarly, pointers to
qualified or unqualified versions of compatible types shall have the same
representation and alignment requirements. All pointers to structure types
shall have the same representation and alignment requirements as each other.
All pointers to union types shall have the same representation and
alignment requirements as each other. Pointers to other types need not have
the same representation or alignment requirements.


Kenneth Brody wrote:


j0mbolar wrote:


for any pointer to T, does a pointer to T have different or can have
different alignment requirement than a pointer to pointer to T? if so,
where is the exact wording in the standard that would suggest so?
I can''t quote chapter and verse, but I can tell you that on the platforms
I currently use:

A pointer to "char" has no alignment restrictions.



A char has no alignment restrictions. A pointer to char may.
A pointer to a 16-bit value must be aligned on a 16-bit boundary.
(ie: the address must be even.)
Pointers are 32 bits, and a pointer-to-pointer must be aligned on
a 32-bit boundary. (ie: the address must be a multiple of 4.)



This is not required, either. In fact, PC-based computers don''t need
(and at least some compilers don''t have) alignment requirements for
pointers and 16-bit ints.

Thad


这篇关于指针对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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