【什么是柔性数组(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. LeetCode算法题-Nth Digit(Java实现)

    这是悦乐书的第215次更新,第228篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第83题(顺位题号是400).找到无限整数序列的第n个数字1,2,3,4,5,6,7,8 ...

  2. spring Environment

    Environment 环境在容器中是一个抽象的集合,是指应用环境的2个方面: profiles和 properties. profile 配置是一个被命名的,bean定义的逻辑组,这些bean只有在 ...

  3. ELMO模型(Deep contextualized word representation)

    1 概述 word embedding 是现在自然语言处理中最常用的 word representation 的方法,常用的word embedding 是word2vec的方法,然而word2vec ...

  4. UART\RS232与RS485的关系

    https://blog.csdn.net/lhl161123/article/details/53510593 串口通讯是电子工程师面对的最基本的一个通讯方式,RS-232是其中最简单的一种.然而, ...

  5. IDEA+'mvn' 不是内部或外部命令

    问题描述: 提示'mvn' 不是内部或外部命令,也不是可运行的程序或批处理文件. 或者提示 The JAVA_HOME environment variable is not defined corr ...

  6. 2018Action Recognition from Skeleton Data via Analogical Generalization over Qualitative Representations

    论文标题: 来源/作者机构情况: Northwestern University Thirty-Second AAAI Conference on Artificial Intelligence, 2 ...

  7. N维偏序:cdq分治

    cdq(陈丹琦)分治,是一种类似二分的算法.基本思想同分治: 递归,把大问题划分成若干个结构相同的子问题,直到(L==R): 处理左区间[L,mid]对右区间[mid+1,R]的影响: 合并. 它可以 ...

  8. redis学习(一)——redis介绍及安装

    一.redis简介 redis是一个高性能的key-value非关系数据库,它可以存键(key)与5种不同类型的值(value)之间的映射(mapping),支持存储的value类型包括:String ...

  9. Google Protocol Buffers学习

    参考资料:http://www.cnblogs.com/royenhome/archive/2010/10/29/1864860.html 参考资料:http://www.jianshu.com/p/ ...

  10. GFF高仿QQ客户端及服务器

    一.GFF简介 GFF是仿QQ界面,通信基于SAEA.MessageSocket.SAEA.Http.SAEA.MVC实现包含客户端和服务器的程序,源码完全公开,项目源码地址:https://gith ...