C99标准的柔性数组 (Flexible Array)

 

【什么是柔性数组(Fiexibel Array)】

柔性数组在C99中的定义是:

6.7.2.1 Structure and union specifiers

As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member.

所以可以给出一个中文定义:

在至少两个成员的结构体中,最后一个成员其类型若是不完整类型的数组类型,则该成员称为柔性数组。

典型例子:

struct s { int n; char str[]; };

注意,str后面的中括号只能为空,数组类型不局限于char。

然而GCC编译器在C99发布之前就支持str[0]作为“柔性数组”,而且str[0]可以放在任何位置。这属于GCC对C语言的语法扩展。

这个语法扩展因为实用并且受欢迎,所以C99将其作为一个特殊情况并得到了支持。下面将具体说明。

 

【柔性数组的由来和作用】

有一些应用场景(如设计数据包)需要一个结构体里面包含因时而异的字符串,没有“柔性数组”概念之前,使用的方法是:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
    struct s { int n; char *str; };
    char string[] = "ZhangHaiba";
    struct char *ptos = malloc(sizeof (struct s) + strlen(string)+1);
    strcpy(ptos+1, string);
    //get the beginning address of str
    char *p = (char *)(ptos+1);

    printf("%s\n", p);
    return 0;
}

GCC超越当时的标准对C语法进行扩展,支持“柔性数组”,就可以使用下面的方法(移植性不好):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
    struct s { int n; char str[0]; };
    char string[] = "ZhangHaiba";
    struct s *ptos = malloc(sizeof (struct s) + strlen(string)+1);
    strcpy(ptos->str, string);
    //get the beginning address of str
    char *p = ptos->str;

    printf("%s\n", p);
    return 0;
}

下面是GCC关于“柔性数组”的官方介绍和对比:http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html

由于上述方法符合C语言的精神(代码简单精炼),因此在C99中得到了支持。所以符合C99标准而且优雅的方法应该是:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
    struct s { int n; char str[]; };
    char string[] = "ZhangHaiba";
    struct s *ptos = malloc(sizeof (struct s) + sizeof (string));
    strcpy(ptos->str, string);
    //get the beginning address of str
    char *p = ptos->str;

    printf("%s\n", p);
    return 0;
}

 

【C99柔性数组的特点】

柔性数组作为不完整类型,即使用struct s test定义了变量test之后,sizeof (test.str)肯定是不行的。哪怕给str分配了空间也不行。

因为标准规定sizeof的操作数不可以是不完整类型(还有函数类型及位字段)。

所以sizeof (struct s)或sizeof (test),不算上柔性数组str占的空间,也是情理之中了。(如果在GCC中,单独测试array[0],则显示其占空间为0字节)

简而言之,柔性数组只是把符号(名字)放在结构体内(压根儿就没定义),方便使用.或->语法来操作柔性数组,其所占内存空间(定义时分配)并不算在结构体变量中。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
    struct s { int n; char str[]; };
    char string[] = "ZhangHaiba";
    struct s *ptos = malloc(sizeof (struct s) + sizeof (string));
    strcpy(ptos->str, string);
    //get the beginning address of str
    char *p = ptos->str;

    printf("%s\n", p);

    struct s test; //test is static allocation,str didn't alloc memory
    printf("%ld %ld\n", sizeof test, sizeof *ptos); // *ptos is dynamic allocation, str has been alloc memory
    //printf("%ld\n", sizeof (test.str)); //error: invalid application of ‘sizeof’ to incomplete type ‘char[]’ 
    //printf("%ld\n", sizeof (ptos->str)); //error: invalid application of ‘sizeof’ to incomplete type ‘char[]’
    return 0;
}

 

@Author: 张海拔

@Update: 2014-2-2

@Link: http://www.cnblogs.com/zhanghaiba/p/3537561.html

 

转载于:https://www.cnblogs.com/zhanghaiba/p/3537561.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值