구조체의 정렬 크기에 대해 구조체의 최대 멤버 크기와 동일한 정렬 크기를 사용해야하는 이유를 알고 싶습니다. , char Data1
후,구조체에 메모리 정렬 - 최대 멤버 크기와 동일한 정렬 크기
struct MixedData /* After compilation in 32-bit x86 machine */
{
char Data1; /* 1 byte */
char Padding1[1]; /* 1 byte for the following 'short' to be aligned on a 2 byte boundary
assuming that the address where structure begins is an even number */
short Data2; /* 2 bytes */
int Data3; /* 4 bytes - largest structure member */
char Data4; /* 1 byte */
char Padding2[3]; /* 3 bytes to make total size of the structure 12 bytes */
};
을하지만 내가 왜이 없습니다 :
예 :
struct MixedData
{
char Data1;
short Data2;
int Data3;
char Data4;
};
우리는 정렬이 4 바이트, 최대 규모의 멤버 (int Data3)
가지고가는 경우에, 그래서 우리가해야 할 char Padding1[3]
이므로 short Data2
은 char Data1[1]
대신 adress(Data1) + 4
에서 시작됩니까?
동일한 로직을 사용하면 이후에 short Padding3[1]
이 표시되지 않는 이유는 무엇입니까?
또 다른 질문 : 저는 64 비트 프로세서입니다 경우, 나는 8 바이트의 alignement를 사용한다, 그래서 나는 다음과 같은 설정해야합니다 :
struct MixedData /* After compilation in 64-bit x86_64 machine */
{
char Data1; /* 1 byte */
char Padding1[7]; /* 7 bytes */
int Data3;
int Padding2[1]/* 4 bytes */
char Data4;
char Padding3[7]; /* 7 bytes to make total size of the structure 24 bytes */
};
?
총 크기가 24 바이트가 8 바이트의 배수입니까?
패딩은 일반적으로 '자연'으로 이루어집니다 : 물론 것
필드를 정렬하십시오. 즉, dword 필드는 dword로 정렬되고, word 필드는 단어로 정렬됩니다. 말하자면, dword 정렬되지는 않지만 (단어 정렬 된) 단어 크기 필드에 액세스 할 경우 패널티가 없습니다. –
@ 500-InternalServerError : 답변 대신 메모로 게시해야합니다. –