English 中文(简体)
为什么要与8保持一致?
原标题:What s the reason to align to 8?
  struct {              /* Fileheader */
    uchar file_version[4];
    uchar options[2];
    uchar header_length[2];
    uchar state_info_length[2];
    uchar base_info_length[2];
    uchar base_pos[2];
    uchar key_parts[2];         /* Key parts */
    uchar unique_key_parts[2];      /* Key parts + unique parts */
    uchar keys;             /* number of keys in file */
    uchar uniques;          /* number of UNIQUE definitions */
    uchar language;         /* Language for indexes */
    uchar max_block_size_index;     /* max keyblock size */
    uchar fulltext_keys;
    uchar not_used;                     /* To align to 8 */
  } header;

以上来源于MySQL,

为什么两人都赞同<代码>8?

问题回答

它是一种优化,能够更有效地利用万国邮联记忆中的结构。

<>Reason>: 地址计算速度更快,规模较小。

在X86和其他一些结构中,如果元素大小是“黑体,圆点数”,则使用阵列元素的效率更高。 关于“种族、圆号”的定义,学习x86组。 但是,你可以看到以下法典在组装上不同尺寸的阵列的效果:

struct s { char c[N]; };
int func(struct s *p, int i) { return p[i].c[0]; }

当N为23岁时(上述结构的大小不dding):

leaq    (%rsi,%rsi,2), %rax
salq    $3, %rax
subq    %rsi, %rax
movsbl  (%rax,%rdi),%eax

当N为24时(上述结构分为dding):

leaq    (%rsi,%rsi,2), %rax
movsbl  (%rdi,%rax,8),%eax

当N为32时(上述结构分为additional):

salq    $5, %rsi
movsbl  (%rsi,%rdi),%eax

发出通知,说明该守则对使用包含23个精细元素的阵列中的一个元素有何复杂性。

<>Reason2: 对于拆卸结构,它允许在档案中的其他部件与配装的负荷和库存相接触。

该结构在软盘上。 在婚后,可以在结构之后直接出现一个32个字。 这使查阅速度更快——编辑自动为记忆结构服务,但需要人工操作光盘的结构。 如果你试图获取不结盟的数据,一些结构甚至会崩溃。

unsigned char *data = ...;
header *h = (header *) data;
do_something_with(h);
uint32_t x = *(uint32_t *) (data + sizeof(header));

The above code will crash a SPARC if sizeof(header) is not a multiple of 4, and on x86 it will be slower unless sizeof(header) is not a multiple of 4.

如果主角是阵列,那么阵列的所有组成部分都将以同样的方式加以统一。 否则,情况并非如此。 你可以研究以下法典/产出,以核实这一点。

如果,Dani 在其评论中正确无误,即用户打算将诸如file_version,那么这种协调非常重要。

Some code

#include <stdio.h>

struct padded {
    unsigned char file_version[4];
    unsigned char options[2];
    unsigned char header_length[2];
    unsigned char state_info_length[2];
    unsigned char base_info_length[2];
    unsigned char base_pos[2];
    unsigned char key_parts[2];
    unsigned char unique_key_parts[2];
    unsigned char keys;
    unsigned char uniques;
    unsigned char language;
    unsigned char max_block_size_index;
    unsigned char fulltext_keys;
    unsigned char not_used;
};

struct unpadded {
    unsigned char file_version[4];
    unsigned char options[2];
    unsigned char header_length[2];
    unsigned char state_info_length[2];
    unsigned char base_info_length[2];
    unsigned char base_pos[2];
    unsigned char key_parts[2];
    unsigned char unique_key_parts[2];
    unsigned char keys;
    unsigned char uniques;
    unsigned char language;
    unsigned char max_block_size_index;
    unsigned char fulltext_keys;
};

int main() {
    printf("size padded:      %lu
", sizeof(struct padded));
    printf("size unpadded:    %lu
", sizeof(struct unpadded));

    printf("size padded[2]:   %lu
", sizeof(struct padded[2]));
    printf("size unpadded[2]: %lu
", sizeof(struct unpadded[2]));
}

The output

size padded:      24
size unpadded:    23
size padded[2]:   48
size unpadded[2]: 46




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签