问题描述
我正在编写一个程序(在C ++中),在其中我需要分配其起始地址应与缓存行大小对齐的数组.当我分配这些数组时,我还希望将内存初始化为零.
I'm writing a program (in C++) in which I need to allocate arrays whose starting addresses should be aligned with the cache line size. When I allocate these arrays I also want the memory initialized to zero.
现在,我可以使用posix_memalign函数来工作了.这对于获取与内存对齐的数组非常有效,但该数组未初始化.在初始化数组时,是否可以使用更好的功能将数组清零?还是只需要编写一个单独的循环来为我做这些准备?
Right now I have it working using the posix_memalign function. This works well for getting memory aligned arrays but the arrays are uninitilized. Is there a better function I can use to zero out the arrays when I initialize them or do I just have to settle for writing a separate loop to do it for me?
推荐答案
只需在块上调用 memset
.确保在调用 memset
之前,不要将指针转换为设置昂贵的类型(例如 char *
).由于您的指针将对齐,因此请确保未从编译器中隐藏信息.
Just call memset
on the block. Make sure you don't cast the pointer to a type that's expensive to set (like char *
) before calling memset
. Since your pointer will be aligned, make sure that information isn't hidden from the compiler.
更新:要澄清我关于不隐藏对齐方式的观点,请比较:
Update: To clarify my point about not hiding alignment, compare:
char* mem_demo_1(char *j)
{ // *BAD* compiler cannot tell pointer alignment, must test
memset(j, 0, 64);
return j;
}
char* mem_demo_2(void)
{ // *GOOD* compiler can tell pointer alignment
char * j = malloc(64);
memset(j, 0, 64);
return j;
}
使用 GCC
, mem_demo_1
可以编译为60行汇编,而 mem_demo_2
可以编译为20行.性能差异也很大.
With GCC
, mem_demo_1
compiles to 60 lines of assembly while mem_demo_2
compiles to 20. The performance difference is also huge.
这篇关于分配初始化的对齐内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!