C语言struct中的长度可变数组(Flexible array member)
C_struct中的长度可变数组(Flexible array member)
Flexible array member is a feature introduced in the C99 standard of the C programming language (in particular, in section §6.7.2.1, item 16, page 103). It is a member of a struct, which is an array without a given dimension, and it must be the last member of such a struct, as in the following example:
struct vectord {
uint8_t len;
double arr[]; // the flexible array member must be last
};
arr[]不占用结构体的存储空间,sizeof(strcut vectord)的值为1- 变长数组必须是结构体的最后一个成员
- 结构体变量相邻的连续存储空间是
arr[]数组的内容 - gcc中使用0长度的数组
arr[0]来表示变长数组。
struct vectord *allocate_vectord (size_t len) {
struct vectord *vec = malloc(offsetof(struct vectord, arr) + len * sizeof(vec->arr[0]));
if (!vec) {
perror("malloc vectord failed");
exit(EXIT_FAILURE);
}
vec->len = len;
for (size_t i = 0; i < len; i++)
vec->arr[i] = 0;
return vec;
}
C语言struct中的长度可变数组(Flexible array member)的更多相关文章
- IOS 中runtime 不可变数组__NSArray0 和__NSArrayI
IOS 中runtime 不可变数组__NSArray0 和__NSArrayI 大家可能都遇到过项目中不可变数组避免数组越界的处理:runtime,然而有时候并不能解决所有的问题,因为类簇不一样 # ...
- 柔性数组成员 (flexible array member)-C99-ZZ
学习flexible array member是因为阅读Redis源码遇到的,sds.h中一开始就用到了. ============================================== ...
- C99标准的柔性数组 (Flexible Array)
[什么是柔性数组(Fliexible Array)] 柔性数组在C99中的定义是: 6.7.2.1 Structure and union specifiers As a special case, ...
- GNU C 中零长度的数组【转】
原文链接:http://www.cnblogs.com/dolphin0520/p/3752492.html 在标准C和C++中,长度为0的数组是被禁止使用的.不过在GNU C中,存在一个非常奇怪的用 ...
- 从零开始学习R语言(四)——数据结构之“数组(Array)”
本文首发于知乎专栏:https://zhuanlan.zhihu.com/p/60141207 也同步更新于我的个人博客:https://www.cnblogs.com/nickwu/p/125677 ...
- OC中动态创建可变数组的问题.有一个数组,数组中有13个元素,先将该数组进行分组,每3个元素为一组,分为若干组,最后用一个数组统一管理这些分组.(要动态创建数组).两种方法
<span style="font-size:24px;">//////第一种方法 // NSMutableArray *arr = [NSMutableArray a ...
- R语言学习笔记:矩阵与数组(array)
元素可以保存在多个维度的对象中,数组存储的是多维数据元素,矩阵的是数组的特殊情况,它具有两维. 创建数组的几种方法. 1. > m<-c(45,23,66,77,33,44,56,12,7 ...
- flexible array柔性数组、不定长的数据结构Struct详解
柔性数组,这个名词对我来说算是比较新颖的,在学习跳跃表的实现时看到的.这么好听的名字,的背后到底是如何的优雅. 柔性数组,其名称的独特和迷惑之处在于“柔性”这个词.在C/C++中定义数组,是一个定长的 ...
- [C] 在 C 语言编程中实现动态数组对象
对于习惯使用高级语言编程的人来说,使用 C 语言编程最头痛的问题之一就是在使用数组需要事先确定数组长度. C 语言本身不提供动态数组这种数据结构,本文将演示如何在 C 语言编程中实现一种对象来作为动态 ...
随机推荐
- Context initialization failed org.springframework.beans.factory.BeanCreationException
严重: Context initialization failed org.springframework.beans.factory.BeanCreationException: Error cre ...
- iOS之创建通知、发送通知和移除通知的坑
1.创建通知,最好在viewDidLoad的方法中创建 - (void)viewDidLoad { [super viewDidLoad]; //创建通知 [[NSNotificationCenter ...
- 『C++』Temp_2018_12_06
#include <iostream> #include <string> using namespace std; class Type{ public: string Na ...
- mongo配置项说明
mongo configure 配置文件 storage: dbPath: mongod实例存储其数据的目录. indexBuildRetry: 指定是否mongod在下次启动时重 ...
- 常用模块 - shutil模块
一.简介 shutil – Utility functions for copying and archiving files and directory trees.(用于复制和存档文件和目录树的实 ...
- Java并发之线程状态及Thread常用方法
本篇文章主要讲解线程的虚拟机状态和线程基本方法,希望可以加深对线程的使用理解. 一.线程的虚拟机状态 线程对象在不同的运行期间有不同的状态,状态信息定义在Thread公共静态枚举java.lang.T ...
- Python-知识点小计
1.python赋值.浅拷贝.深拷贝区别:https://www.cnblogs.com/xueli/p/4952063.html: 2.python的hasattr(),getattr(),sett ...
- yii学习笔记(7),数据库操作,联表查询
在实际开发中,联表查询是很常见的,yii提供联表查询的方式 关系型数据表:一对一关系,一对多关系 实例: 文章表和文章分类表 一个文章对应一个分类 一个分类可以对应多个文章 文章表:article 文 ...
- To Support High-Density Retina Displays
http://www.sitepoint.com/support-retina-displays/ http://www.leemunroe.com/designing-for-high-resolu ...
- C# WebBrowser的DrawToBitmap方法 截取网页保存为图片
bool mark = true; private void btnOpen_Click(object sender, EventArgs e) { ...