分配在给定边界上对齐的内存 [英] Allocating memory aligned on a given boundary

查看:59
本文介绍了分配在给定边界上对齐的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得malloc()的结果,使得它在给定的边界上对齐 - 8,在这种情况下。假设sizeof(u64)

为8,那么


u64 * p = malloc(N);


一般来说这个诀窍吗?


这里N是一个正整数,我假设malloc()

成功分配了指定的内存块。


这似乎有用,但我不知道它只是我的

编译器,还是C中的一般规则。

I would like to get the result of malloc() such that it is
aligned on a given boundary - 8, in this case. Assuming that sizeof(u64)
is 8, will

u64 *p = malloc(N) ;

do the trick in general?

Here N is a positive integer, and I am assuming that malloc()
succeeds in allocating the chunk of memory specified.

This seems to work, but I do not know whether it is just my
compiler, or a general rule in C.

推荐答案

K.詹宁斯说:
K. Jennings said:

我想获得malloc()的结果,以便在给定的边界上对齐

- 8,在这种情况下。假设

sizeof(u64)为8,将


u64 * p = malloc(N);


一般来说诀窍?
I would like to get the result of malloc() such that it is
aligned on a given boundary - 8, in this case. Assuming that
sizeof(u64) is 8, will

u64 *p = malloc(N) ;

do the trick in general?



C标准保证malloc'的结果(如果不是NULL)总是

正确对齐任何对象类型。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh位于上述域名, - www。

The C Standard guarantees that malloc''s result (if not NULL) is always
correctly aligned for any object type.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


文章< 7K ******* ***********************@bt.com>,

Richard Heathfield< rj*@see.sig.invalidwrote:
In article <7K******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:

> K.詹宁斯说:
>K. Jennings said:


>我想得到malloc()的结果,这样它就是
在给定边界上对齐 - 在这种情况下为8。假设
sizeof(u64)为8,则
>I would like to get the result of malloc() such that it is
aligned on a given boundary - 8, in this case. Assuming that
sizeof(u64) is 8, will


> u64 * p = malloc(N);
>u64 *p = malloc(N) ;


>做一般的伎俩?
>do the trick in general?


> C标准保证malloc的结果(如果不是NULL)始终正确对齐任何对象类型。
>The C Standard guarantees that malloc''s result (if not NULL) is always
correctly aligned for any object type.



稍微扩大:


这意味着如果特定的实施能够和

愿意处理(比如说)4字节边界上的u64'(和其他所有内容),

然后malloc(sizeof u64)不保证在多个对齐上$
u64的大小。保证只是它将在

上分配一个可访问的边界。可访问边界

和特定对齐边界(例如,8)之间的差异有时可能是b
关注的问题,例如,如果您尝试考虑缓存效果。

-

有什么可说的,看,这是新的吗?它已经过时了,这已经是我们面前的了。 - Ecclesiastes

Expanding slightly:

This means that if the particular implementation is able and
willing to handle u64''s (and everything else) on (say) 4 byte boundaries,
then malloc(sizeof u64) is not guaranteed to be aligned on a multiple
of sizeof u64. The guarantee is only that it will be allocated on
an accessible boundary. The difference between accessible boundaries
and specific alignment boundaries (e.g., 8) can sometimes be of
concern, such as if you are trying to take into account cache effects.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes


Richard Heathfield写道,2007年4月24日17:41:
Richard Heathfield wrote, On 24/04/07 17:41:

K.詹宁斯说:
K. Jennings said:

>我想获得malloc()的结果,使得它在给定的边界上对齐 - 8,在此案件。假设
sizeof(u64)为8,那么u64 * p = malloc(N);

一般的做法是什么?
>I would like to get the result of malloc() such that it is
aligned on a given boundary - 8, in this case. Assuming that
sizeof(u64) is 8, will

u64 *p = malloc(N) ;

do the trick in general?



C标准保证malloc'的结果(如果不是NULL)总是

正确对齐任何对象类型。


The C Standard guarantees that malloc''s result (if not NULL) is always
correctly aligned for any object type.



但是它不能保证对齐可能是什么,也不能确定任何

的方法。因此,如果你真的想要一个特定的对齐方式,例如

,这样你就可以使用一些高度系统特定的技巧,你将需要使用一些高度系统特定的方法。任何这样的方法和技巧

都不在这里,所以你必须在一个小组中询问

感兴趣的特定平台。

-

Flash Gordon

However it makes no guarantees about what the alignment might be nor any
way to find out. So if you really want a specific alignment, for example
so that you can use some highly system specific tricks, you will need to
use some highly system specific method. Any such methods and tricks
would be off topic here so you would have to ask in a group dealing with
the specific platform(s) of interest.
--
Flash Gordon


这篇关于分配在给定边界上对齐的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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