首页 > 其他 > 详细

零长数组

时间:2014-03-11 10:29:20      阅读:351      评论:0      收藏:0      [点我收藏+]

GCC 中允许使用零长数组(__GNUC__ >= 3)。

零长数组在有固定头部的可变对象上非常适用,我们可以根据对象的大小动态地去分配结构体的大小。

gui ftk里经常用的。在分配内存时,零长数组指向变长的PrivInfo。

FTK_ZALLOC(sizeof(FtkSource) + sizeof(PrivInfo))

 

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
#if __GNUC__ >= 3
#define ZERO_LEN_ARRAY 0
#else
#define ZERO_LEN_ARRAY 1
#endif
  
struct _FtkSource
{
    FtkSourceGetFd    get_fd;
    FtkSourceCheck    check;
    FtkSourceDispatch dispatch;
    FtkSourceDestroy  destroy;
   
    int  ref;
    int  disable;
    char priv[ZERO_LEN_ARRAY];
};
   
typedef struct _PrivInfo
{
    FtkTimer action;
    void* user_data;
    size_t interval;
    size_t next_time;
}PrivInfo;
   
FtkSource* ftk_source_timer_create(int interval, FtkTimer action, void* user_data)
{
    FtkSource* thiz = NULL;
    return_val_if_fail(interval > 0 && action != NULL, NULL);
   
    thiz = (FtkSource*)FTK_ZALLOC(sizeof(FtkSource) + sizeof(PrivInfo));
    if(thiz != NULL)
    {
        DECL_PRIV(thiz, priv);
        //PrivInfo* priv = PrivInfo*(thiz->priv);         //zlz comment
        thiz->get_fd   = ftk_source_timer_get_fd;
        thiz->check    = ftk_source_timer_check;
        thiz->dispatch = ftk_source_timer_dispatch;
        thiz->destroy  = ftk_source_timer_destroy;
   
        thiz->ref = 1;
        priv->interval  = interval;
        priv->user_data = user_data;
        priv->action    = action;
        ftk_source_timer_calc_timer(priv);
    }
   
    return thiz;
}

demo

 

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifdef __cplusplus
extern "C"
{
#endif
  
#include <stdio.h>
#include <string.h>
 
#if __GNUC__ >= 3
 
#define ZERO_LEN_ARRAY 0
#else
#define ZERO_LEN_ARRAY 1
#endif
 
typedef struct taglcgi_msg_head
{
    //MessageHead mh;
    int  is_return;
    int  result;
    int  reason;
    char user[32];
    char ipaddr[16];
    //char priv[0];
}LCGI_MSG_HEAD, *PLCGI_MSG_HEAD;
  
typedef struct taglcgi_msg_head0
{
    //MessageHead mh;
    int  is_return;
    int  result;
    int  reason;
    char user[32];
    char ipaddr[16];
    char priv[0];       //不占空间,用的时候才占
}LCGI_MSG_HEAD0, *PLCGI_MSG_HEAD0;
  
typedef struct taglcgi_msg_head1
{
    //MessageHead mh;
    int  is_return;
    int  result;
    int  reason;
    char user[32];
    char ipaddr[16];
    char priv[1];       //大小/功能都等同于void* priv
}LCGI_MSG_HEAD1, *PLCGI_MSG_HEAD1;
  
typedef struct taglcgi_msg_head2
{
    //MessageHead mh;
    int  is_return;
    int  result;
    int  reason;
    char user[32];
    char ipaddr[16];
    void* priv;             //大小/功能都等同于void* priv
}LCGI_MSG_HEAD2, *PLCGI_MSG_HEAD2;
   
int main(int argc,char* argv[])
{
    printf("sizeof(LCGI_MSG_HEAD)  len %d\n", sizeof(LCGI_MSG_HEAD));      
    printf("sizeof(LCGI_MSG_HEAD0) len %d\n", sizeof(LCGI_MSG_HEAD0));     
    printf("sizeof(LCGI_MSG_HEAD1) len %d\n", sizeof(LCGI_MSG_HEAD1));     
    printf("sizeof(LCGI_MSG_HEAD2) len %d\n", sizeof(LCGI_MSG_HEAD2));
     
    char *p ="123";
    //char const *p ="123";
    p = "3214545";
    //p[1] = ‘d‘;
    printf("p %s\n", p);
         
    return 0;
}
  
#ifdef __cplusplus
}
#endif

 



零长数组,布布扣,bubuko.com

零长数组

原文:http://www.cnblogs.com/freezlz/p/3590148.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!