如何以可移植的方式强制执行指针对齐(即w / 64位指针) [英] How to enforce pointer alignment in a portable way (i.e. w/ 64 bit pointers)

查看:89
本文介绍了如何以可移植的方式强制执行指针对齐(即w / 64位指针)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

构造

(void *)(((long)ptr + 3)& ~3)

一直运行到现在为止强制指针对齐长期

边界。但是,现在VC ++发出警告,无疑是为了帮助44位机器工作,即64位指针。


在C的早期,这里有大小问题,即
为16或32位,响应是int保证持有

a指针(是的,有64Kb地址)空间一次!)。但是,我知道这个约定违反了现代的

编译器,所以

(void *)((int) ptr + 3)& ~3)

也不起作用。是否有一种可以随意逼迫指针的方式,

无论大小如何?

The construct
(void*)(((long)ptr + 3) & ~3)
worked well until now to enforce alignment of the pointer to long
boundaries. However, now VC++ warns about it, undoubtedly to help things
work on 64 bit machines, i.e. with 64 bit pointers.

In the early days of C, where there were problems with the size of int
being 16 or 32 bits, the response was that an int was guaranteed to hold
a pointer (yes, there were 64Kb address spaces at one time!). However, I
understand that this convention is being violated with the modern
compilers, so that
(void*)(((int)ptr + 3) & ~3)
doesn''t work either. Is there an portable way of coercing pointers,
regardless of their size?

推荐答案



2004年7月8日星期四,Ken Turkowski写道:

On Thu, 8 Jul 2004, Ken Turkowski wrote:

构造
(void *)(((long)ptr + 3)& ~3)
到目前为止一直很好地强制指针指向长边界。


1989年没有工作,今天不工作。获得一个更好的C教程。

然而,现在VC ++警告它,无疑是为了帮助在64位机器上工作,即64位指针。


对他们有好处。

在C的早期,有16或32位的大小问题,响应是int保证保持指针(是的,一次有64Kb的地址空间!)。但是,我明白现在的编译器正在违反这个约定,所以
(void *)(((int)ptr + 3)& ~3)
也不起作用。是否有一种可移植的方法来强制指针,
无论大小如何?

The construct
(void*)(((long)ptr + 3) & ~3)
worked well until now to enforce alignment of the pointer to long
boundaries.
Didn''t work in 1989, doesn''t work today. Get a better C tutorial.
However, now VC++ warns about it, undoubtedly to help things
work on 64 bit machines, i.e. with 64 bit pointers.
Good for them.
In the early days of C, where there were problems with the size of int
being 16 or 32 bits, the response was that an int was guaranteed to hold
a pointer (yes, there were 64Kb address spaces at one time!). However, I
understand that this convention is being violated with the modern
compilers, so that
(void*)(((int)ptr + 3) & ~3)
doesn''t work either. Is there an portable way of coercing pointers,
regardless of their size?




当然不是。幸运的是,你不需要。如果你认为你这样做,那么
然后解释你的问题,我们可以尝试帮助你解决它。但是

担心指针对齐的唯一原因是,如果你实现''malloc'和朋友,那么你就不会实现
,在这种情况下你不会需要

担心可移植性(并且有一些众所周知的技巧可以帮助你应对

''union')。


发布您的真实问题,如果您有的话。如果你只是对b / b
好​​奇关于指针的按位技巧,答案更简单:

指针不允许按位技巧。算了吧。


HTH,

-Arthur



Of course not. Luckily, you don''t need to. If you think you do,
then explain your problem, and we can try to help you solve it. But
the only reason to be worried about pointer alignment is if you''re
implementing ''malloc'' and friends, and in that case you don''t need
to worry about portability (and there are well-known tricks with
''union'' to help you cope).

Post your real problem, if you''ve got one. If you were just
curious about bitwise tricks on pointers, the answer is even simpler:
Pointers don''t allow bitwise tricks. Forget it.

HTH,
-Arthur


Ken Turkowski< tu ** @ worldserver.com>写道:
Ken Turkowski <tu**@worldserver.com> writes:
是否有一种可移植的方法来强制指针,无论它们的大小如何?
Is there an portable way of coercing pointers, regardless of
their size?




intptr_t和uintptr_r是

< stdint.h>中定义的C99整数类型,保证大到足以容纳指针,但

它们是可选的。

-

当我不得不依靠不足之处时,我更喜欢它是我自己的。

- 理查德希思菲尔德



intptr_t and uintptr_r are C99 integer types defined in
<stdint.h>that are guaranteed large enough to hold a pointer, but
they are optional.
--
"When I have to rely on inadequacy, I prefer it to be my own."
--Richard Heathfield


文章

< Pi **************************** *******@unix50.andr ew.cmu.edu>,

" Arthur J. O''Dwyer" < aj*@nospam.andrew.cmu.edu>写道:
In article
<Pi***********************************@unix50.andr ew.cmu.edu>,
"Arthur J. O''Dwyer" <aj*@nospam.andrew.cmu.edu> wrote:
2004年7月8日星期四,Ken Turkowski写道:
On Thu, 8 Jul 2004, Ken Turkowski wrote:

构造
(void *)((( long)ptr + 3)& ~3)
到目前为止一直很好地强制指针对齐长边界。

The construct
(void*)(((long)ptr + 3) & ~3)
worked well until now to enforce alignment of the pointer to long
boundaries.



没有工作在1989年,今天不工作。获得一个更好的C教程。



Didn''t work in 1989, doesn''t work today. Get a better C tutorial.




相反,你每天使用的很多软件都使用这样的构造

。 />



On the contrary, a lot of the software you use every day uses constructs
like this.

但是,现在VC ++对此发出警告,无疑是为了帮助64位机器工作,即使用64位指针。
However, now VC++ warns about it, undoubtedly to help things
work on 64 bit machines, i.e. with 64 bit pointers.



对他们有好处。



Good for them.

在C的早期,int的大小有16或32位的问题,响应是int保证保持指针(是的,一次有64Kb的地址空间!)。但是,我明白现在的编译器正在违反这个约定,所以
(void *)(((int)ptr + 3)& ~3)
也不起作用。是否有一种可移植的方法来强制指针,
无论大小如何?
In the early days of C, where there were problems with the size of int
being 16 or 32 bits, the response was that an int was guaranteed to hold
a pointer (yes, there were 64Kb address spaces at one time!). However, I
understand that this convention is being violated with the modern
compilers, so that
(void*)(((int)ptr + 3) & ~3)
doesn''t work either. Is there an portable way of coercing pointers,
regardless of their size?



当然不是。幸运的是,你不需要。如果你认为你这样做,那么
然后解释你的问题,我们可以尝试帮助你解决它。但是,担心指针对齐的唯一原因是你是否实现''malloc'和朋友,在这种情况下你不需要担心可移植性(并且有一些众所周知的技巧可以帮助你应对'



发布您真正的问题,如果你有的话。如果你只是对指针上的点滴技巧感到好奇,那么答案就更简单了:
指针不允许有点诡计。忘掉它。



Of course not. Luckily, you don''t need to. If you think you do,
then explain your problem, and we can try to help you solve it. But
the only reason to be worried about pointer alignment is if you''re
implementing ''malloc'' and friends, and in that case you don''t need
to worry about portability (and there are well-known tricks with
''union'' to help you cope).

Post your real problem, if you''ve got one. If you were just
curious about bitwise tricks on pointers, the answer is even simpler:
Pointers don''t allow bitwise tricks. Forget it.




当你使用

更大的数据类型时,将一个图像复制到另一个图像的速度会更快。例如,如果源和目标以8字节
对齐重合,则可以执行

double * f,* t; long n;

...设置......

而(n--)

* t ++ = * f ++;

复制双打而不是
字节更快,但速度不快8倍。



The speed of copying one image to another is faster when you use a
larger data type. If the source and destinations coincide in 8-byte
alignment, for example, you can do
double *f, *t; long n;
... setup ...
while (n--)
*t++ = *f++;
It is faster, though not quite 8X faster, to copy doubles rather than
bytes.


这篇关于如何以可移植的方式强制执行指针对齐(即w / 64位指针)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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