Il tipo di ciascun membro della struttura di solito ha un allineamento predefinito. Il membro della struttura i.e.each è allineato su un limite predeterminato. Per questo motivo l'imbottitura è eseguita nel seguente esempio wiki:Perché favorire l'allineamento della struttura dei dati?
struct MixedData
{
char Data1;
short Data2;
int Data3;
char Data4;
};
struct MixedData /* After compilation in 32-bit x86 machine */
{
char Data1; /* 1 byte */
/* 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 */
char Padding1[1];
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 */
};
Qual è la (pratico) ragione che l'allineamento deve essere conservato?
possibile duplicato di [Perché C++ non rende la struttura più stretta?] (Http://stackoverflow.com/questions/6730664/why-doesnt-c-make-the-structure-tighter) –
Fare riferimento a questo link. Ti aiuterà a comprendere in che modo l'allineamento rende possibile un accesso veloce. http://www.geeksforgeeks.org/archives/9705 –