[筆記]Unions 在C語言的簡單介紹

References:
1. http://publications.gbdirect.co.uk/c_book/chapter6/unions.html

OS      : Ubuntu 11.10 x86_64
gcc      : 4.6.1


      最近寫C語言用到了union這個 structure,一開始還不是很懂在作什麼,所以看了網路上的一些範例以及說明,以下是依照我的理解來說明。

如果文章對你有幫助在幫我按一下廣告來讓我有額外收入這也是對我來說是一種鼓勵

簡單的說就是union裏面的變數是共用同一個記憶體,資料是以最後一筆寫入變數為主,而且分配的記憶體是以最大的資料結構做為分配的依據。所以我用以下的範例來說明,當最後一筆資料是寫入un_double所以之前un_char,un_int所寫的入資料將會被換掉,畢竟是使用同一塊記憶體。在記憶體配置的部份,最大是double的size是8bytes,所以ex_type的size是8bytes。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <stdarg.h>
struct ex_type{
      union{
              double   un_double;
              char    un_char;
              int     un_int;
      };
};
 
int main(int argc , char ** argv){
    
    struct ex_type tt ;
    tt.un_char  = 'a';
    tt.un_int  = 10;
    tt.un_double = 10.5;
    
    printf("struct ex_type size = %d\n",sizeof(struct ex_type));
    printf("double size = %d\n",sizeof(double));
    printf("char   size = %d\n",sizeof(char));
    printf("int    size = %d\n",sizeof(int));
    printf("un_double  = %f\n",tt.un_double);
    printf("un_char  = %c\n",tt.un_char);
    printf("un_int   = %d\n",tt.un_int);
    return 0;
}

struct ex_type size = 8
double size = 8
char   size = 1
int    size = 4
un_double  = 10.500000
un_char  = 
un_int   = 0


       以下的範例是用struct重新定義新的資料型態,我定義的_coffe跟_car只有差在_car有用union去包裡面的struct。然後將這兩個資料型態實現出來並依照同樣的順序將資料寫入a,b,c,d以及wheel_a,wheel_b,wheel_c,wheel_d然後全部將結果show出來。

       接著_coffe所定義出來的size是16(因為兩個struct大小為8bytes ,8+8 = 16byte ),所以變數a,b,c,d有各自的4bytes記憶體空間。

      _car定義出來的size為8(因為兩個struct大小為8bytes),當我資料是依順寫入10,11,12,13到wheel_a,wheel_b,wheel_c,wheel_d,會發現最後的資料只有12,13所以之前的10,11的資料已經被蓋掉了,由結果可以資料兩個struct是共用8bytes(拆成兩個4bytes)的記憶體空間。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <stdarg.h>
 
#define show(...) \
    printf(__VA_ARGS__);
struct _coffe
{
    struct
    {
        int a;
        int b;
    };
    struct
    {
        int c;
        int d;
    };
};
struct _car
{
    union
    {
        struct
        {
            int wheel_a;
            int wheel_b;
        }; 
        struct 
        { 
            int wheel_c;
            int wheel_d;
        };
    };
};
int main(int argc, char ** argv)
{
    struct _coffe *coffe = (struct _coffe *) malloc(sizeof(struct _coffe)) ;
    coffe->a = 10; 
    coffe->b = 11; 
    coffe->c = 12; 
    coffe->d = 13;
    show("struct _coffe size = %ld\n",sizeof(struct _coffe)); 
    show("coffe_a = %d , address = %x\n",coffe->a,&coffe->a);
    show("coffe_b = %d , address = %x\n",coffe->b,&coffe->b);
    show("coffe_c = %d , address = %x\n",coffe->c,&coffe->c);
    show("coffe_d = %d , address = %x\n",coffe->d,&coffe->d);
    
    show("=====我是分隔線=====\n"); 
    
    struct _car *wheel = (struct _car *) malloc(sizeof(struct _car)) ;
    wheel->wheel_a = 10; 
    wheel->wheel_b = 11; 
    wheel->wheel_c = 12; 
    wheel->wheel_d = 13;
 
    show("struct _car size = %ld\n",sizeof(struct _car)); 
    show("wheel_a = %d , address = %x\n",wheel->wheel_a,&wheel->wheel_a);
    show("wheel_b = %d , address = %x\n",wheel->wheel_b,&wheel->wheel_b);
    show("wheel_c = %d , address = %x\n",wheel->wheel_c,&wheel->wheel_c);
    show("wheel_d = %d , address = %x\n",wheel->wheel_d,&wheel->wheel_d);
 
    return 0;
}

struct _coffe size = 16
coffe_a = 10 , address = 836b008
coffe_b = 11 , address = 836b00c
coffe_c = 12 , address = 836b010
coffe_d = 13 , address = 836b014
=====我是分隔線=====
struct _car size = 8
wheel_a = 12 , address = 836b020
wheel_b = 13 , address = 836b024
wheel_c = 12 , address = 836b020
wheel_d = 13 , address = 836b024





留言

熱門文章