一開始本來再查甚麼是ABI (Appilcation Binary Interface)
其中有一點是
ABIs cover details such as
- the sizes, layout, and alignment of data types
有點忘記甚麼叫做alignment所以就花了 一點時間去查一下
那為什麼會有alignment這個名詞出現呢?
因為變數再宣告的時候,會配置給他記憶體,但每種大小不一樣,比如說long 64 bit ,int 32bit , cahr 8bit,此時就產生了一個問題,那要怎麼樣塞進記憶體之後要取資料比較方便呢?
一般來說再32-bit的架構上我們一次會讀4個byte的資料出來,所以如果此時資料沒有放好我們會讀很多次才會讀到我們想要的資料,所以就有alignment這個方法的出現。
所以我就仿造我查到的網誌真的動手去試試看
int main (void)
{
struct ali
{
char x; //1byte
int y;//4bytes
short int z;//2 bytes
}align_ex;
printf("align_ex size is %d\n",sizeof(align_ex));
printf("x size is %d\n",sizeof(align_ex.x));
printf("y size is %d\n",sizeof(align_ex.y));
printf("z size is %d\n",sizeof(align_ex.z));
printf("address of test is %p\n", &align_ex);
printf("address of x is %p\n", &align_ex.x);
printf("address of y is %p\n", &align_ex.y);
printf("address of z is %p\n", &align_ex.z);
}
真的會幫我配置好,只不過會有內部碎裂,看到的專業名詞叫做
:"padding"(幫你insert 空的資料 讓data可以對齊)
如果我改成以下 就會有不一樣的結果,或者順序也會影響記憶體配置
int main (void)
{
struct ali
{
char x;//1byte
char y;//1bytes
int z;//4 bytes
}align_ex;
printf("align_ex size is %d\n",sizeof(align_ex));
printf("x size is %d\n",sizeof(align_ex.x));
printf("y size is %d\n",sizeof(align_ex.y));
printf("z size is %d\n",sizeof(align_ex.z));
printf("address of test is %p\n", &align_ex);
printf("address of x is %p\n", &align_ex.x);
printf("address of y is %p\n", &align_ex.y);
printf("address of z is %p\n", &align_ex.z);
}
reference :
1.http://kezeodsnx.pixnet.net/blog/post/27585076-data-structure%E7%9A%84%E5%B0%8D%E9%BD%8A(alignment)
2. http://www.programmer-club.com.tw/ShowSameTitleN/c/38584.html
The Casino Directory | JtmHub
回覆刪除The Casino Directory is a gri-go.com complete งานออนไลน์ directory for casino and sportsbook operators in 출장안마 Ireland and Portugal. Jtm's comprehensive directory provides you with novcasino more than 150