什么是用于alignas和alignof关键字? [英] What are the alignas and alignof keywords used for?

查看:965
本文介绍了什么是用于alignas和alignof关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解的目的 alignas alignof 关键字,我不能肯定我完全明白对齐是什么。

I'm having trouble understand what the purpose of the alignas and alignof keywords are, and I'm not quite sure I fully understand what alignment is.

据我了解,一个内存地址是通过计数'N'字节的时间对齐到n个字节,如果它是被n整除,也就是说,它可以得到(从0?或者一些默认值?) 。此外, alignas 关键字,当prefixing变量声明,规定了如何在该变量的存储地址是对齐,以及 alignof 返回一个变量的地址的对齐方式。

As I understand it, a memory address is aligned to n bytes if it is divisible by n, that is, it can be got to by counting 'n' bytes at a time (from 0? or some default value?). Also, the alignas keyword, when prefixing a variable declaration, specifies how the address at which the variable is stored is to be aligned, and the alignof returns how a variable's address is aligned.

不过,我没有信心,这是对齐或 alignof / alignas 关键字正确的认识 - 请纠正我任何我错之分。我也看不出有什么使用这些关键字的发球,所以我会AP preciate它,如果任何人都可以指出他们的目的是什么。

However, I am not confident that this is a correct understanding of alignment or the alignof/alignas keywords - please correct me on any of the points I got wrong. I also don't see what use these keywords serve, so I would appreciate it if anyone could point out what their purpose is.

推荐答案

一些特殊类型必须比usual-例如更多的字节对齐,矩阵必须在16字节对齐在x86,最有效的复制到GPU。 SSE矢量类型可以表现也是这样的。因此,如果你想使一个容器类型,那么你必须知道你在试图遏制或分配的类型的对齐要求。

Some special types must be aligned at more bytes than usual- for example, matrices must be aligned at 16bytes on x86 for the most efficient copying to the GPU. SSE vector types can behave this way too. As such, if you want to make a container type, then you must know the alignment requirements of the type you're trying to contain or allocate.

这篇关于什么是用于alignas和alignof关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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