【什么是柔性数组(Fliexible 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)+);
strcpy(ptos+, string);
//get the beginning address of str
char *p = (char *)(ptos+); printf("%s\n", p);
return ;
}

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

#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) + strlen(string)+);
strcpy(ptos->str, string);
//get the beginning address of str
char *p = ptos->str; printf("%s\n", p);
return ;
}

下面是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 ;
}

【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 ;
}

@Author: 张海拔

@Update: 2014-2-2

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

C99标准的柔性数组 (Flexible Array)的更多相关文章

  1. C语言struct中的长度可变数组(Flexible array member)

    C_struct中的长度可变数组(Flexible array member) Flexible array member is a feature introduced in the C99 sta ...

  2. flexible array柔性数组、不定长的数据结构Struct详解

    柔性数组,这个名词对我来说算是比较新颖的,在学习跳跃表的实现时看到的.这么好听的名字,的背后到底是如何的优雅. 柔性数组,其名称的独特和迷惑之处在于“柔性”这个词.在C/C++中定义数组,是一个定长的 ...

  3. 柔性数组-读《深度探索C++对象模型》有感 (转载)

    最近在看<深度探索C++对象模型>,对于Struct的用法中,发现有一些地方值得我们借鉴的地方,特此和大家分享一下,此间内容包含了网上搜集的一些资料,同时感谢提供这些信息的作者. 原文如下 ...

  4. 柔性数组-读《深度探索C++对象模型》有感

    最近在看<深度探索C++对象模型>,对于Struct的用法中,发现有一些地方值得我们借鉴的地方,特此和大家分享一下,此间内容包含了网上搜集的一些资料,同时感谢提供这些信息的作者. 原文如下 ...

  5. 深入浅出C语言中的柔性数组

    在日常的编程中,有时候需要在结构体中存放一个长度动态的字符串,一般的做法,是在结构体中定义一个指针成员,这个指针成员指向该字符串所在的动态内存空间,例如: typedef struct test { ...

  6. 柔性数组(Redis源码学习)

    柔性数组(Redis源码学习) 1. 问题背景 在阅读Redis源码中的字符串有如下结构,在sizeof(struct sdshdr)得到结果为8,在后续内存申请和计算中也用到.其实在工作中有遇到过这 ...

  7. 柔性数组成员 (flexible array member)-C99-ZZ

    学习flexible array member是因为阅读Redis源码遇到的,sds.h中一开始就用到了. ============================================== ...

  8. c99柔性数组

    变长结构体 struct test { int nSize; char data[]; // 或者 char data[0];但建议使用 char data[]; 注意:c98 时不支持柔性数组,其仅 ...

  9. 变长数组(variable-length array,VLA)(C99)

    处理二维数组的函数有一处可能不太容易理解,数组的行可以在函数调用的时候传递,但是数组的列却只能被预置在函数内部.例如下面这样的定义: #define COLS 4 int sum3d(int ar[] ...

随机推荐

  1. JavaScript -- 时光流逝(九):Window 对象、Navigator 对象

    JavaScript -- 知识点回顾篇(九):Window 对象.Navigator 对象 1. Window 对象 1.1 Window 对象的属性 (1) closed: 返回窗口是否已被关闭. ...

  2. Windows 命令行

     1.d: 进入d盘  2.dir(directory)   显示当前文件夹下的所有内容  3.md (make directory) +文件夹名 在当前文件夹下创建文件夹  4.rd (remove ...

  3. C++_调用约束

    1.要求 声明定义处调用约定必须相同 int __stdcall add(int a, int b); int __stdcall add(int a, int b) {  return a + b; ...

  4. java操作elasticsearch实现查询删除和查询所有

    后期博客本人都只给出代码,具体的说明在代码中也有注释. 1.查询删除 //查询删除:将查询到的数据进行删除 @Test public void test8() throws UnknownHostEx ...

  5. Word中的通配符随意组合进行批量替换或删除某些内容

    长文档需要批量修改或删除某些内容的时候,我们可以利用Word中的通配符来搞定这一切,当然,前提是你必须会使用它.通配符的功能非常强大,能够随意组合替换或删除我们定义的规则内容,下面易老师就分享一些关于 ...

  6. Angular中ui-router实现路由嵌套案例

    学习 ui-router 资料整理 对于Angular内置的路由是单路由视图,ui-router可以实现路由嵌套.后面将会有一个案例概括前面所有资料整理 学习 ui-router - 管理状态  ht ...

  7. python学习初始函数

    函数的用途:解决代码的冗余.可读性差.可扩展性差. 函数的一般格式: #函数定义 def mylen(): """计算s1的长度""" s1 ...

  8. mybatis generator使用方式

    资源: 一 https://files.cnblogs.com/files/jiuya/jdbcT.zip 二  https://files.cnblogs.com/files/jiuya/mybat ...

  9. [SDOi2012]吊灯

    嘟嘟嘟 这题想了半天,搞出了一个\(O(10 * d * n)\)(\(d\)为\(n\)的约数个数)的贪心算法,就是能在子树内匹配就在子树内匹配,否则把没匹配的都交给父亲,看父亲能否匹配.交上去开了 ...

  10. js如何获取跨域iframe 里面content

    每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 其中src可能存在跨域. 现有的获取方式   var test = document. ...