C99标准的柔性数组 (Flexible Array)
【什么是柔性数组(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)的更多相关文章
- C语言struct中的长度可变数组(Flexible array member)
C_struct中的长度可变数组(Flexible array member) Flexible array member is a feature introduced in the C99 sta ...
- flexible array柔性数组、不定长的数据结构Struct详解
柔性数组,这个名词对我来说算是比较新颖的,在学习跳跃表的实现时看到的.这么好听的名字,的背后到底是如何的优雅. 柔性数组,其名称的独特和迷惑之处在于“柔性”这个词.在C/C++中定义数组,是一个定长的 ...
- 柔性数组-读《深度探索C++对象模型》有感 (转载)
最近在看<深度探索C++对象模型>,对于Struct的用法中,发现有一些地方值得我们借鉴的地方,特此和大家分享一下,此间内容包含了网上搜集的一些资料,同时感谢提供这些信息的作者. 原文如下 ...
- 柔性数组-读《深度探索C++对象模型》有感
最近在看<深度探索C++对象模型>,对于Struct的用法中,发现有一些地方值得我们借鉴的地方,特此和大家分享一下,此间内容包含了网上搜集的一些资料,同时感谢提供这些信息的作者. 原文如下 ...
- 深入浅出C语言中的柔性数组
在日常的编程中,有时候需要在结构体中存放一个长度动态的字符串,一般的做法,是在结构体中定义一个指针成员,这个指针成员指向该字符串所在的动态内存空间,例如: typedef struct test { ...
- 柔性数组(Redis源码学习)
柔性数组(Redis源码学习) 1. 问题背景 在阅读Redis源码中的字符串有如下结构,在sizeof(struct sdshdr)得到结果为8,在后续内存申请和计算中也用到.其实在工作中有遇到过这 ...
- 柔性数组成员 (flexible array member)-C99-ZZ
学习flexible array member是因为阅读Redis源码遇到的,sds.h中一开始就用到了. ============================================== ...
- c99柔性数组
变长结构体 struct test { int nSize; char data[]; // 或者 char data[0];但建议使用 char data[]; 注意:c98 时不支持柔性数组,其仅 ...
- 变长数组(variable-length array,VLA)(C99)
处理二维数组的函数有一处可能不太容易理解,数组的行可以在函数调用的时候传递,但是数组的列却只能被预置在函数内部.例如下面这样的定义: #define COLS 4 int sum3d(int ar[] ...
随机推荐
- Nunit单元测试入门学习随笔(一)
Nunit单元测试 一.插件安装与项目关联 选择工具~扩展和更新 点击联机~搜索Nunit安装图内三个插件 新建单元测试项目 勾选项目引用 二.Nunit学习 1.了解单元测试 单元测试在我的理解是测 ...
- 《Java大学教程》—第10章 图形和事件驱动程序
10.2 Swing程序包AWT(Abstract Window Toolkit): 抽象窗口工具集.依赖于本地操作系统的组件被称为重量级(Heavy Weight)组件,因为它们都会大量使用系统资源 ...
- Teradata超长数据会截断
1.数据库版本 Teradata 15.10 2.测试案例: create multiset table test_stg ( col1 ) CHARACTER SET LATIN not null ...
- 16.ajax_case02
# 抓取当当网书评 # http://product.dangdang.com/25340451.html import json import requests from lxml import e ...
- 两段锁协议(Two-Phase Locking――2PL)
两段锁协议(Two-Phase Locking――2PL) 两段锁协议规定所有的事务应遵守的规则: ① 在对任何数据进行读.写操作之前,首先要申请并获得对该数据的封锁. ② 在释放一个封锁之后,事务不 ...
- UVA1374-Power Calculus(迭代加深搜索)
Problem UVA1374-Power Calculus Accept:323 Submit:2083 Time Limit: 3000 mSec Problem Description I ...
- Visual Studio Code 支持TensorFlow配置支持
首先选择解释器 选择TensorFlow版本的conda版本 (当然你如果是通过python单独安装的TensorFlow也可以) 编辑器输入代码,进行测试 import tensorflow as ...
- python 那些我记不清的函数
eval 函数:用来计算在字符串中的有效Python表达式,并返回一个对象......将字符串变回数据类型 enumerate函数:加上序号 isinstance函数:判断数据类型 isinstanc ...
- go第三方日志系统-seelog-使用文档
参考:https://godoc.org/github.com/cihub/seelog 导入方式: import "github.com/cihub/seelog" 包seelo ...
- Spring Security(二十二):6.4 Method Security
From version 2.0 onwards Spring Security has improved support substantially for adding security to y ...