如何分配2 ^ k对齐的内存 [英] How to allocate memory which is 2^k aligned
问题描述
有人可以帮我搞清楚如何编写函数
其中inturn使用malloc例程来分配内存,这是2 ^ k >
对齐?
条件是这样的,不应该有任何内存泄漏,它不会浪费内存。
也不应该浪费内存。
谢谢,
Divick
Hi,
can somebody help me figure out how can I make write a function
which inturn uses malloc routine to allocate memory which is 2^k
aligned?
The condition is such that there should not be any memory leak and it
should not also waste memory.
Thanks,
Divick
推荐答案
Divick写道:
Divick wrote:
有人可以帮我弄清楚如何编写函数
其中inturn使用malloc例程来分配内存,这是2 ^ k
对齐?
条件是不应该有任何内存泄漏而且它不应该浪费内存。
can somebody help me figure out how can I make write a function
which inturn uses malloc routine to allocate memory which is 2^k
aligned?
The condition is such that there should not be any memory leak and it
should not also waste memory.
我想你想从一个数字计算下一个2的幂。
鉴于你需要分配N个字节,N不一定是2的任何2美元,所以你需要找到不小于2的下一个2的幂。比N更贵。你可以在一个循环。
V
-
请在通过电子邮件回复时删除资金''A' />
我没有回复最热门的回复,请不要问
I think you''re looking to calculate the next power of 2 from a number.
Given that you need to allocate N bytes, the N is not necessarily any
power of 2, so you need to find the next power of 2 that is not smaller
than N. You can do it in a loop.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
Divick发布:
Divick posted:
有人可以帮我搞清楚如何编写函数
其中inturn使用malloc例程来分配内存,这是2 ^ k
对齐?
Can somebody help me figure out how can I make write a function
which inturn uses malloc routine to allocate memory which is 2^k
aligned?
" malloc"这已经做了 - 它分配了对齐严格的内存
足以满足任何类型对象的对齐。
您可能会喜欢aligned_storage。 aswell(Google for it),虽然它是
分配在堆栈上而不是堆上。
-
Frederick Gotham
"malloc" does this already -- it allocates memory whose alignment is strict
enough to satisfy the alignment of any kind of object.
You might like "aligned_storage" aswell (Google for it), although it
allocates on the stack rather than on the heap.
--
Frederick Gotham
Divick写道:
Divick wrote:
>
can有人帮我搞清楚如何写一个函数
哪个inturn使用malloc例程来分配2 ^ k
对齐的内存?
>
条件是不应该有任何内存泄漏并且它不会浪费内存。
>
can somebody help me figure out how can I make write a function
which inturn uses malloc routine to allocate memory which is 2^k
aligned?
The condition is such that there should not be any memory leak and it
should not also waste memory.
For或多或少k的任意值,唯一的方法是
分配一个肯定足够大的块(即2 ^(k + 1))并选择
关闭该块内满足对齐要求的地址。
对于k的值,使得2 ^ k是某种类型的对齐(通常是
2,4,8或16个字节),malloc返回一个'恰当的阿里指针' g.55
任何类型,所以你不需要做任何特别的事。
-
- 皮特
标准C ++库扩展:一个教程和
参考的作者。有关本书的更多信息,请参阅
www.petebecker.com/tr1book。
这篇关于如何分配2 ^ k对齐的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!