struct

本文部分轉載自http://www.cnblogs.com/qyaizs/articles/2039101.html,在此感謝做者!html

 1 首先://注意在C和C++裏不一樣
    在C中定義一個結構體類型要用typedef:
    typedef struct Student
    {
    int a;
    }Stu;
    因而在聲明變量的時候就可:Stu stu1;(若是沒有typedef就必須用struct Student stu1;來聲明)
    這裏的Stu實際上就是struct Student的別名。Stu==struct Student
    另外這裏也能夠不寫Student(因而也不能struct Student stu1;了,必須是Stu stu1;)
    typedef struct
    {
    int a;
    }Stu;
    但在c++裏很簡單,直接
    struct Student
    {
    int a;
    };    
    因而就定義告終構體類型Student,聲明變量時直接Student stu2;
======================================================================================

  2.其次:
    在c++中若是用typedef的話,又會形成區別:
    struct   Student   
    {   
    int   a;   
    }stu1;//stu1是一個變量  ios

 
    typedef   struct   Student2   
    {   
    int   a;   
    }stu2;//stu2是一個結構體類型=struct Student  c++

 
    使用時能夠直接訪問stu1.a
    可是stu2則必須先   stu2 s2;
    而後               s2.a=10;spa

 

 typedef struct和struct的區別:code

 

 

    typedef struct tagMyStruct
    {
     int iNum;
     long lLength;
    } MyStruct;
htm

    上面的tagMyStruct是標識符,MyStruct是變量類型(至關於(int,char等))。blog

 

 

    這語句實際上完成兩個操做:內存

      1) 定義一個新的結構類型編譯器

    struct tagMyStruct
    {  
     int iNum;
     long lLength;
    };
string

  分析:tagMyStruct稱爲「tag」,即「標籤」,其實是一個臨時名字,不管是否有typedefstruct 關鍵字和tagMyStruct一塊兒,構成了這個結構類型,這個結構都存在。

  咱們能夠用struct tagMyStruct varName來定義變量,但要注意,使用tagMyStruct varName來定義變量是不對的,由於struct 和tagMyStruct合在一塊兒才能表示一個結構類型。

  2) typedef爲這個新的結構起了一個名字,叫MyStruct。

    typedef struct tagMyStruct MyStruct;

  所以,MyStruct實際上至關於struct tagMyStruct,咱們可使用MyStruct varName來定義變量。

  2.

    typedef struct tagMyStruct
    {
     int iNum;
     long lLength;
    } MyStruct;

    在C中,這個申明後申請結構變量的方法有兩種:

    (1)struct tagMyStruct 變量名

    (2)MyStruct 變量名

    在c++中能夠有

    (1)struct tagMyStruct 變量名

    (2)MyStruct 變量名

    (3)tagMyStruct 變量名

 

2016-10-23 17:23:16

 

計算機組成原理教導咱們,這樣有助於加快計算機的取數速度,不然就得多花指令週期了。

 

爲此,編譯器默認會對結構體進行處理(實際上其它地方的數據變量也是如此),讓寬度爲2的基本數據類型(short等)都位於能被2整除的地址上,讓寬度爲4的基本數據類型(int等)都位於能被4整除的地址上。
字節對齊的細節和編譯器實現相關,但通常而言,知足三個準則:
    1) 結構體變量的首地址可以被其最寬基本類型成員的大小所整除;
    2) 結構體每一個成員相對於結構體首地址的偏移量(offset)都是成員大小的整數倍,若有須要編譯器會在成員之間加上填充字節(internal adding);
    3) 結構體的總大小爲結構體最寬基本類型成員大小的整數倍,若有須要編譯器會在最末一個成員以後加上填充字節(trailing padding)。

 

 

結構體對齊問題,結構體以體內字段的內存佔用數最大的字段爲基本對齊基礎。

指定對齊方式

可使用#pragma pack(N)來指定結構體成員的對齊方式
對於指定的對齊方式,其成員的地址偏移以及結構的總的大小也有下面三個約束條件

    1. 結構體第一個成員的地址和結構體的首地址相同
    2. 結構體每一個成員的地址偏移須要知足:N大於等於該成員的大小,那麼該成員的地址偏移需知足默認對齊方式(地址偏移是其成員大小的整數倍);N小於該成員的大小,那麼該成員的地址偏移是N的整數倍。(結構體成員對齊的規則以下: 將本身的自己在內存中實際佔用的字節和當前由#pragma pack(n)設定的n進行比較,取其中最下的那個做爲結構體當前成員的對齊方式,可是不影響其餘結構體成員的對齊方式。)
    3. 結構體總的大小須要時N的整數倍,若是不是須要在結構體的末尾進行填充。
    4. 若是N大於結構體成員中最大成員的大小,則N不起做用,仍然按照默認方式對齊。

 

 

 

 

 

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <errno.h>
 4 #include <stdlib.h>
 5 #include <signal.h>
 6 #include <string.h>
 7 #include <iostream>
 8 #include <sys/wait.h>
 9 #pragma pack(2)//#pragma pack(n)這個預處理,主要的功能是改變內存對齊方式的選項,按照給定的n字節進行內存對齊
10 using namespace std;
11 struct test{
12     char a;
13     int b;
14     short c;
15 }test;
16 struct test1{
17     char a;
18     long b;
19     short c;
20     char d;
21 }test1;
22 struct test2{
23    char a;
24    short b;
25    char c;
26    long d;
27 }test2;
28 struct buffer
29 {
30     long data_len;   //長度
31     char d;
32     char data[0];  //起始地址
33 }buffer;
34 int main()
35 {
36     printf("test$$$$$$$$$$$$$$$:%lu\n", sizeof(test));//12
37     printf("%#x %#x %#x\n", &test.a, &test.b, &test.c);
38     printf("test1@@@@@@@@@@@@@:%lu\n", sizeof(test1));//24
39     printf("%#x %#x %#x %#x\n", &test1.a, &test1.b, &test1.c, &test1.d);
40     printf("test2###########:%lu\n", sizeof(test2));//16
41     printf("%#x %#x %#x %#x\n", &test2.a, &test2.b, &test2.c, &test2.d);
42     printf("buffer############:%lu\n", sizeof(buffer));//16
43     printf("%#x %#x %#x\n", &buffer.data_len, &buffer.d, &buffer.data);
44 
45     return 0;
46 }

 

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <stdlib.h>
 4 #include <string.h>
 5 #include <iostream>
 6 using namespace std;
 7 typedef struct test0{
 8     char a[3];
 9     int b;
10     char c;
11 }sttest0;
12 typedef struct test1{
13     char a[4];
14     int b;
15     char c;
16 }sttest1;
17 typedef struct test2{
18     char a[7];
19     int b;
20     char c;
21 }sttest2;
22 typedef struct test3{
23     char a[8];
24     int b;
25     char c;
26 }sttest3;
27 typedef struct test4{
28     char a[9];
29     int b;
30     char c;
31 }sttest4;
32 int main()
33 {
34     printf("%lu %lu %lu %lu %lu\n", sizeof(sttest0), sizeof(sttest1), sizeof(sttest2), sizeof(sttest3), sizeof(sttest4));
35 
36     printf("address$$$$$$$$$$$$$$$$$\n");
37     sttest0 t0;
38     sttest1 t1;
39     printf("%p %p %p\n", &t0.a, &t0.b, &t0.c);
40     printf("%p %p %p\n", &t1.a, &t1.b, &t1.c);
41     return 0;
42 }

 

 

相關文章
相關標籤/搜索