溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C語言中結(jié)構(gòu)體struct怎么對(duì)齊

發(fā)布時(shí)間:2021-08-11 19:00:35 來源:億速云 閱讀:178 作者:chen 欄目:關(guān)系型數(shù)據(jù)庫

這篇文章主要介紹“C語言中結(jié)構(gòu)體struct怎么對(duì)齊”,在日常操作中,相信很多人在C語言中結(jié)構(gòu)體struct怎么對(duì)齊問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”C語言中結(jié)構(gòu)體struct怎么對(duì)齊”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

struct,相互關(guān)聯(lián)的元素的集合,每個(gè)元素都有自己的內(nèi)存空間;每個(gè)元素在內(nèi)存中的存放是有先后順序的,就是定義時(shí)候的順序;一個(gè)struct所占的總的內(nèi)存大小,并不是各個(gè)元素所占空間之和,而是存在字節(jié)對(duì)齊的問題.
struct中的每個(gè)元素相對(duì)于結(jié)構(gòu)體的首地址的偏移量能被該元素的size整除(某些編譯器,如果該元素的size > 4,則偏移量能被4整除即可).
測試代碼:

[xdb@localhost test]$ cat test.cpp
#include <cstdio>
#include <iostream>
using namespace std;
#define LL long long 
struct E1 {
    int a; char b; char c;
}e1;
struct E2 {
    char b; int a; char c;
}e2;
struct E3 {
    char a; short b; int c; LL d;
}e3;
struct E4 {
    int c; LL d; char a; short b;
}e4;
struct E5 {
    char a1,a2,a3,a4,a5,a6;
}e5;
struct E6 {
    char a1,a2,a3;
}e6;
struct E7 {
    struct E5 elem5;
    struct E6 elem6;
    LL a;
}e7;
struct E8 {
    char a[9];
}e8;
struct E9 {
    struct E8 elem8;
    LL a;
}e9;
struct E10 {
    char a;
};
int main() {
    puts("----> E1");
    cout << sizeof(E1) << endl;
    printf("%x %x %x %x\n", &e1, &e1.a, &e1.b, &e1.c);    
    puts("----> E2");
    cout << sizeof(E2) << endl;
    printf("%x %x %x %x\n", &e2, &e2.b, &e2.a, &e2.c);    
    puts("----> E3");
    cout << sizeof(E3) << endl;
    printf("%x %x %x %x %x\n", &e3, &e3.a, &e3.b, &e3.c, &e3.d);    
    puts("----> E4");
    cout << sizeof(E4) << endl;
    printf("%x %x %x %x %x\n", &e4, &e4.c, &e4.d, &e4.a, &e4.b);    
    puts("----> E5");
    cout << sizeof(E5) << endl;
    puts("----> E6");
    cout << sizeof(E6) << endl;
    puts("----> E7");
    cout << sizeof(E7) << endl;
    printf("%x %x %x %x\n", &e7, &e7.elem5, &e7.elem6, &e7.a);
    puts("----> E8");
    cout << sizeof(E8) << endl;
    puts("----> E9");
    cout << sizeof(E9) << endl;
    printf("%x %x %x\n", &e9, &e9.elem8, &e9.a);
    puts("----> E10");
    cout << sizeof(E10) << endl;
    return 0;
}
[xdb@localhost test]$

編譯,執(zhí)行

[xdb@localhost test]$ g++ test.cpp -o test
[xdb@localhost test]$ ./test
----> E1
8
6021a0 6021a0 6021a4 6021a5
----> E2
12
6021a8 6021a8 6021ac 6021b0
----> E3
16
6021c0 6021c0 6021c2 6021c4 6021c8
----> E4
24
6021d0 6021d0 6021d8 6021e0 6021e2
----> E5
6
----> E6
3
----> E7
24
602200 602200 602206 602210
----> E8
9
----> E9
24
602230 602230 602240
----> E10
1
[xdb@localhost test]$

到此,關(guān)于“C語言中結(jié)構(gòu)體struct怎么對(duì)齊”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI