到底什么是“对齐指针”?

发布于 2024-10-05 07:54:57 字数 37 浏览 0 评论 0原文

有人可以告诉我对齐指针实际上意味着什么吗?

Would somebody please tell me what an aligned pointer actually means?

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

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

发布评论

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

评论(6

偏爱你一生 2024-10-12 07:54:57

这意味着所指向的地址可以被某个因子整除。

有时使用术语“自然对齐”,这通常意味着具有自然对齐的对象需要放置在可被对象大小整除的地址处。

对齐有时非常重要,因为许多与硬件相关的事物对这种对齐设置了限制。

例如,在经典的 SPARC 架构上(以及经典的 ARM,我认为),您无法从奇数地址读取大于一字节的整数。尝试这样做会立即因总线错误而停止您的程序。在 x86 架构上,CPU 硬件反而会处理该问题(通过根据需要对缓存和/或内存进行多次访问),尽管可能需要更长时间。 RISC:ier 架构通常不会为您执行此操作。

类似的事情也会影响填充,即在结构字段之间插入虚拟数据以保持对齐。像这样的结构:

struct example
{
  char initial;
  double coolness;
};

很可能最终在字段之间有 7 个字节的填充,以使 double 字段在可被其自身大小整除的偏移量上对齐(我假设为 8) 。

当以二进制形式查看时,与 n 字节对齐的地址将其 log2(n) 最低有效位设置为零。例如,需要 32 字节对齐的对象将具有以(二进制)00000 结尾的正确对齐地址,因为 log2(32) 为 5。这也意味着可以通过清除所需的数字来强制地址对齐位。

It means that the address being pointed at is evenly divisible by some factor.

Sometimes the term "natural alignment" is used, which generally means that objects having natural alignment need to be placed at addresses that are evenly divisble by the object's size.

Alignment is somestimes very important, since many hardware-related things place restrictions on such alignment.

For instance, on the classic SPARC architecture (and also on classical ARM, I think), you can't read an integer larger than one byte from an odd address. Trying to do so will immediately halt your program with a bus error. On the x86 architecture, the CPU hardware instead handles the problem (by doing multiple accesses to cache and/or memory as needed), although it might take longer. RISC:ier architectures typically don't do this for you.

Things like these can also affect padding, i.e. the insertion of dummy data between e.g. structure fields in order to maintain alignment. A structure like this:

struct example
{
  char initial;
  double coolness;
};

would very likely end up having 7 bytes of padding between the fields, to make the double field align on an offset divisible by its own size (which I've assumed to be 8).

When viewed in binary, an address aligned to n bytes will have its log2(n) least-significant bits set to zero. For instance, an object that requires 32-byte alignment will have a properly-aligned address that ends with (binary) 00000, since log2(32) is 5. This also implies that an address can be forced into alignment by clearing the required number of bits.

泪之魂 2024-10-12 07:54:57

为了补充 unwind 所解释的内容,这是我最近在作业中使用的一个 struct

struct infosale {               
    int   noseq;                
    char  salesman[30];         
    char  product[11];          
    int   count;                
};                               

您可能期望此struct的大小为(4+30+11+4=)49字节,但实际上是52sizeof 相比。因为 noseq4 字节 + salesman32 字节(对齐)+ product12 字节(对齐),而 count4 字节,因此是 52 字节。

To add to what unwind is explaining, here is a struct I have recently used in an assignment :

struct infosale {               
    int   noseq;                
    char  salesman[30];         
    char  product[11];          
    int   count;                
};                               

You may expect the size of this struct to be (4+30+11+4=) 49 bytes, but it is in fact 52 compared with sizeof. Because noseq is 4 bytes + salesman is 32 bytes (aligned) + product is 12 bytes (aligned) and count is 4 bytes, thus 52 bytes.

七禾 2024-10-12 07:54:57

取决于上下文,但可能是指针本身被对齐,或者它指向的内容被对齐。

“对齐”意味着某个对象存储在某个常量的倍数的地址处。例如,对于 32 位整数,这几乎总是 4。这是因为一个字节是 8 位:4*8 = 32 位。通常,如果对象存储在对齐的地址处,处理器可以执行更快的内存访问,或者对于某些处理器来说,甚至不可能执行未对齐的访问。

Depends on the context, but it could either be the pointer itself being aligned, or what it points to is aligned.

'Aligned' means that a certain object is stored at a address which is a multiple of a certain constant. E.g. for 32-bit integers this is almost always 4. This is because a byte is 8-bits: 4*8 = 32-bit. Often a processor can do much faster memory access if the object is stored at an aligned address, or for some processors it's even not possible to do unaligned accesses.

伴我老 2024-10-12 07:54:57

它是一个指向“对齐”地址的指针。对齐的意思是地址是某个值的倍数 - 通常是它将指向的任何类型的事物的大小(如果是原始类型),或者需要这种对齐的某些数据成员的大小。

通常您不必担心这一点;内存分配函数将确保它们提供给您的内存正确对齐。当你开始使用指针强制转换做不安全的事情时,你就开始担心对齐问题。

It is a pointer to an "aligned" address. Aligned in the sense that the address is a multiple of some value - typically, the sizeof whatever type of thing it will be pointing at (if a primitive type), or of some data member which requires such alignment.

Usually you do not have to worry about this; memory allocation functions will ensure that the memory they give you is properly aligned. You start worrying about alignment at the point where you start doing unsafe things with pointer casts.

柒夜笙歌凉 2024-10-12 07:54:57

正如人们在我之前提到的,这意味着你的指针可以被一定数量的字节整除。

要检查指针是否对齐,可以执行以下操作:

isaligned = !( (long)pointer % bytes );

现在,如果“指针”与“字节”字节对齐,则“isaligned”为真。

As people have mentioned before me it means that your pointer is evenly divisible by a certain number of bytes.

To check if your pointer is aligned you can do this:

isaligned = !( (long)pointer % bytes );

Now, "isaligned" is true if "pointer" is aligned to "bytes" bytes.

属性 2024-10-12 07:54:57

对齐指针表示具有相邻内存位置的指针,可以通过添加常量及其倍数来访问

char a[5] = "12345";

这里 a 是常量指针,如果您每次可以访问下一个字符,则将 char 的大小指向它,即

a+sizeofchar 将访问 2

a+ ( sizeofchar*2 ) 将访问 3 等。

如果您逐位访问变量值,

Aligned Pointer means that pointer with adjacent memory location that can be accessed by a adding a constant and its multiples

for char a[5] = "12345";

here a is constant pointer if you and the size of char to it every time you can access the next chracter that is,

a+sizeofchar will access 2

a+( sizeofchar*2 ) will access 3 an so on

similarly if you access the varible bit by bit values.

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