alignas 和alignof 关键字的用途是什么?

发布于 2024-11-29 08:19:03 字数 370 浏览 0 评论 0原文

我无法理解 alignasalignof 关键字的用途,而且我不太确定我完全理解什么是对齐。

据我了解,如果内存地址可被n整除,则它与n个字节对齐,也就是说,可以通过一次计算“n”个字节(从0?或某个默认值?)来获得它。此外,alignas 关键字在变量声明前面添加前缀时,指定如何对齐存储变量的地址,alignof 返回变量的地址如何对齐对齐。

但是,我不确定这是对对齐或 alignof/alignas 关键字的正确理解 - 请纠正我的任何错误点。我也不明白这些关键字有什么用,所以如果有人能指出他们的目的是什么,我将不胜感激。

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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟织青萝梦 2024-12-06 08:19:03

某些特殊类型必须以比平常更多的字节对齐 - 例如,矩阵必须在 x86 上以 16 字节对齐,以便最有效地复制到 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.

深爱不及久伴 2024-12-06 08:19:03

据我了解,如果内存地址可被n整除,则它与n个字节对齐,也就是说,可以通过一次计算“n”个字节来获取它(从0?或某个默认值?) .

是的,它通常与 0 全等,因为它是硬件对齐,类似于(旧)硬盘柱面的对齐(请参阅 气缸头扇区)。好吧,我不确定我对圆柱体的看法是否正确;我对硬件不热衷。

我从未使用过alignas,但显然它对于优化与某些硬件的交互很有用。例如,虽然它不是针对 RAM 的,但在 gcc 的手册页中,您可以看到 -falign-* 优化选项,并解释了它们的用途:对齐一些代码以便 CPU 可以一次获取更多指令。

对于常见的标量类型,通常会将 sizeof(your_type) 等于 alignof(your_type),这是为了优化。但对于复合类型(数组或结构),该值将有所不同,通常等于成员之一所需的最大对齐方式。

不考虑 RAM 之外的更多硬件,这对于某些通用数据管理功能非常有用。当然,像malloc()这样的分配函数总是给你一个对任何类型都方便的地址,也就是说,为max_align_t对齐,它通常有一个alignof 为 16(根据 long double 的要求)。但今年,我参与了一个项目,该项目仅使用一个 malloc() 来分配一个矩阵指向矩阵每一行的指针,在同一个块中,例如,int * * 类型的变量。因此,该块应首先包含 int * 指针的空间,然后包含 int 值的空间。因为(在常见系统上)alignof(int *) %alignof(int) == 0,这种情况下是没有问题的。但在 32 位架构上,例如,使用 double 或 int64_t 会失败,因为指针是 4 字节长,并且值需要 8 对齐。必须使用alignof来确定值的正确开始(当然,填充必须添加到分配的大小中)。就我而言,这还没有完成,这不是问题,因为该程序仅在 64 位架构上进行了测试,并且没有使用 long double 或类似的东西。但我已经完成了修复,因此该程序肯定可以在 32 位机器上运行。

如果我理解正确的话,如果您有以下条件,则 alignof(type) 相当于 offsetof(struct char_and_type, item)

struct char_and_type {
    char c;
    type item;
};

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?).

Yes, it is normally congruent to 0, because it is a hardware alignment, similar to an alignment on cylinders of (old) hard disks (see Cylinder-head-sector). Well, I’m not sure I’m right with this things of cylinders; I’m not keen on hardware.

I’ve never used alignas, but clearly it can be useful for optimization in interaction with some hardware. For instance, although it’s not for RAM, in the man page of gcc you can see the -falign-* optimization options, with an explanation of their purpose: align some code in the output so that the CPU can fetch more instructions at once.

With common scalar types, you will normally have sizeof(your_type) being equal to alignof(your_type), which is for optimization. But for a compound type (array or struct), the value will be different and usually equal to the maximum alignment required by one of the members.

Without regard for more hardware than the RAM, this can be useful for some generic data management functions. Of course, allocation functions such as malloc() always give you an address which is convenient for any type, that is, aligned for max_align_t which usually has an alignof of 16 (as required by long double). But this year, I have worked on a project which uses only one malloc() to allocate a matrix and pointers to each row of the matrix, in the same block, for a variable of type int * * for example. So the block shall contain first the space for the int * pointers and then the space for the int values. Because (on common systems) alignof(int *) % alignof(int) == 0, there is no problem in this case. But on a 32-bit architecture, it would fail with double or int64_t for example, because the pointers are 4-bytes long and the values require an alignment of 8. So alignof must be used to determine a correct start for the values (and the padding must be added to the allocated size, of course). In my case, this was not done yet, and it was not a problem because the program was only tested on 64-bit architectures and did not use long double or alike. But I’ve done the fix, so that the program can surely work on 32-bit machines.

If I understand correctly, alignof(type) is equivalent to offsetof(struct char_and_type, item) if you have:

struct char_and_type {
    char c;
    type item;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文