便携式对齐 [英] Portable alignof

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

问题描述

思考,评论?


#ifdef HAVE_TYPEOF


/ *

*可以使用任意表达式< br $>
* /

#define alignof(t)\

((sizeof(t)> 1)?offsetof(struct {char c ; typeof(t)x;},x):1)


#else


/ *

*只能使用类型

* /

#define alignof(t)\

((sizeof(t)> 1)? offsetof(struct {char c; tx;},x):1)


#endif

Thoughts, comments?

#ifdef HAVE_TYPEOF

/*
* Can use arbitrary expressions
*/
#define alignof(t) \
((sizeof (t) > 1)? offsetof(struct { char c; typeof(t) x; }, x) : 1)

#else

/*
* Can only use types
*/
#define alignof(t) \
((sizeof (t) > 1)? offsetof(struct { char c; t x; }, x) : 1)

#endif

推荐答案

William Ahern< wi ***** @ 25thandClement.com>写道:
William Ahern <wi*****@25thandClement.com> writes:
#define alignof(t)\
((sizeof(t)> 1)?offsetof(struct {char c; tx;},x) :1)
#define alignof(t) \
((sizeof (t) > 1)? offsetof(struct { char c; t x; }, x) : 1)




我不确定这是否正常或不正确,但是我必须说,无论是否b $ b ,它非常聪明。

-

适合主人的适合新手。

你必须在超越结构之前理解道。

- 编程之道



I''m not certain whether that''s portably correct or not, but I
must say that, regardless, it''s remarkably clever.
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming


William Ahern< wi ***** @ 25thandClement.com>写道:
William Ahern <wi*****@25thandClement.com> writes:
思考,评论?

#ifdef HAVE_TYPEOF

/ *
*可以使用任意表达式
* /
#define alignof(t)\
((sizeof(t)> 1)?offsetof(struct {char c; typeof(t)x;},x):1)

#else

/ *
*只能使用类型
* /
#define alignof(t)\
(( sizeof(t)> 1)?offsetof(struct {char c; tx;},x):1)

#endif
Thoughts, comments?

#ifdef HAVE_TYPEOF

/*
* Can use arbitrary expressions
*/
#define alignof(t) \
((sizeof (t) > 1)? offsetof(struct { char c; typeof(t) x; }, x) : 1)

#else

/*
* Can only use types
*/
#define alignof(t) \
((sizeof (t) > 1)? offsetof(struct { char c; t x; }, x) : 1)

#endif




我没有看到它的问题,但我不认为测试

sizeof(t)是必要的:


# define alignof(t)\

offsetof(struct {char c; tx;},x)


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



I don''t see a problem with it, but I don''t think the test for
sizeof(t) is necessary:

#define alignof(t) \
offsetof(struct { char c; t x; }, x)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


William Ahern写道:
William Ahern wrote:
思考,评论?
......
# define alignof(t)\
((sizeof(t)> 1)?offsetof(struct {char c; tx;},x):1)
Thoughts, comments?
...
#define alignof(t) \
((sizeof (t) > 1)? offsetof(struct { char c; t x; }, x) : 1)




看起来没问题,但是意识到虽然它会返回一个合适的

对齐,但是它可能不会返回最小对齐。例如,一个4字节的
int可能只需要在给定的机器上进行两个字节对齐,但由于编译器相关的填充设置,

,alignof可能是

返回4(甚至8等...)。


你的测试是否sizeof(t)> 1表示你已经

在这方面遇到了char这样的问题。


如果你是偏执狂,你想要更精确一点,你可能

想要取最小的表达式和sizeof(t)...


#define min(a,b)((a) )<(b)?(a):( b))


#define simple_alignof(t)\

(sizeof(t)> 1?offsetof(struct {char c; tx;},x):1)


#define alignof(t)min(sizeof(t),simple_alignof(t))


-

彼得



Looks okay, but realise that whilst it will return a suitable
alignment,
it may not return the minimum alignment. For instance, a 4 byte
int may only require two byte alignment on a given machine, but
because of compiler dependant padding settings, alignof might
return 4 (or even 8 etc...).

Your test if sizeof(t) > 1 would indicate that you have already
encountered such an issue with char in that regard.

If you''re paranoid and you want a little more precision, you might
want to take the minimum of your expression and sizeof(t)...

#define min(a,b) ((a) < (b) ? (a) : (b))

#define simple_alignof(t) \
(sizeof (t) > 1 ? offsetof(struct { char c; t x; }, x) : 1)

#define alignof(t) min(sizeof(t), simple_alignof(t))

--
Peter


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

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