柔性数组
在C99标准之后,在结构体中最后一个元素允许是未知大小的,称之为柔性数组
- 一般定义为:
- 类型 变量名[]
- 注意:
- 结构体中,柔性数组前必须有变量
- 柔性数组必须定义在结构体的最后
- 示例:
struct CoolArr_s{
int len;
char arr[];
};
- -
- 如果编译器报错,则考虑把最后的 char arr[] 尝试替换为一下几种写法:
- char arr[0]
- char arr[1] (非常不推荐,如果这样声明后使用时会被认为是越界行为)
- 如果编译器报错,则考虑把最后的 char arr[] 尝试替换为一下几种写法:
- 使用 malloc(sizeof(int) + sizeof(char) * 长度) 申请内存空间
- 示例:
#include "stdio.h"
#include "stdlib.h"
struct CoolArr_s
{
int len;
char arr[];
};
int main()
{
//这里给arr的长度为10个char的大小
struct CoolArr_s* p = (struct CoolArr_s*)malloc(sizeof(int) + sizeof(char) * 10);
for (int i = 10; i--;)
{
(p->arr)[i] = i + '0';
printf("%d " ,(p->arr)[i]);
}
printf("\n CoolArr_s: %d, *p: %d\n", sizeof(struct CoolArr_s), sizeof(*p));
return 0;
}
- 运行结果:
- 那么这和直接把它定义为指针有什么区别呢?或者说它有什么好处呢?
- 首先从上面的例子的运行结果可见,结构体 CoolArr_s 的大小就是 int len 的大小。也就是说,柔性数组 char arr[] 并不会占一个指针的大小。数组名 arr 是一个常量地址,这个地址是紧接着 int len 的后面的地址。(数组名和指针是不同的)
- 其次就是柔性数组的内存空间是和结构体连续的,而声明为指针时,其指向的数组内存空间和结构体极大概率不是连续的。这一点在给一些函数传值时是非常重要的区别,比如 linux中的消息队列 相关的函数。
在C++中的柔性数组
注意:在c++中并不建议使用柔性数组,而应考虑使用array代替。即使它可以使用在struct和class中,但部分编译器是不支持这种操作的。
- 截止至2022(C++20标准):
- 在g++中是可以使用柔性数组的
- 在VS2022的MSVC中使用柔性数组,虽然可以运行,但会有警告:
- warning C4200: 使用了非标准扩展: 结构/联合中的零大小数组
- warning C6011: 取消对 NULL 指针 “xxx” 的引用
- 所以在c++中应考虑使用array容器代替。
- 示例(使用array容器代替上面的例子):
#include <iostream>
#include <array>
using namespace std;
int main()
{
//数组长度
const int len_arr = 10;
//定义数组
array<int, len_arr> arr;
//array::data()返回数组的首地址
//array::begin()返回的是迭代器iterator
int *arr_p = arr.data();
for (int i = len_arr; i--;)
{
arr_p[i] = i;
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
mexico drug stores pharmacies medicine in mexico pharmacies medicine in mexico pharmacies
mexican pharmacy mexican border pharmacies shipping to usa pharmacies in mexico that ship to usa
best online pharmacies in mexico medicine in mexico pharmacies medicine in mexico pharmacies
medicine in mexico pharmacies mexico drug stores pharmacies mexican pharmaceuticals online
http://mexicanpharmacy.cheap/# mexican online pharmacies prescription drugs
http://mexicanpharmacy.cheap/# mexican border pharmacies shipping to usa
https://mexicanpharmacy.cheap/# mexican rx online
buying from online mexican pharmacy buying from online mexican pharmacy mexico pharmacy
mexico pharmacy mexican pharmaceuticals online medicine in mexico pharmacies
mexican mail order pharmacies buying from online mexican pharmacy mexico pharmacies prescription drugs
http://mexicanpharmacy.cheap/# buying from online mexican pharmacy
gГјnstige online apotheke: kamagra tabletten – online apotheke preisvergleich
http://kamagrakaufen.top/# internet apotheke
https://viagrakaufen.store/# Viagra online bestellen Schweiz
online apotheke preisvergleich kamagra jelly kaufen deutschland online apotheke versandkostenfrei
https://kamagrakaufen.top/# internet apotheke
gГјnstige online apotheke online apotheke gunstig online-apotheken
http://viagrakaufen.store/# Viagra Alternative rezeptfrei
online apotheke deutschland: kamagra oral jelly kaufen – п»їonline apotheke
http://potenzmittel.men/# gГјnstige online apotheke
internet apotheke versandapotheke deutschland online apotheke preisvergleich
http://kamagrakaufen.top/# online apotheke deutschland
Viagra online bestellen Schweiz: viagra bestellen – Viagra Alternative rezeptfrei
https://kamagrakaufen.top/# internet apotheke
п»їonline apotheke cialis rezeptfreie kaufen online apotheke deutschland
https://kamagrakaufen.top/# gГјnstige online apotheke
online apotheke preisvergleich п»їonline apotheke versandapotheke versandkostenfrei
https://kamagrakaufen.top/# online apotheke preisvergleich
https://kamagrakaufen.top/# versandapotheke
online apotheke versandkostenfrei: kamagra oral jelly kaufen – versandapotheke versandkostenfrei
online-apotheken Online Apotheke Deutschland online apotheke preisvergleich
versandapotheke versandkostenfrei п»їonline apotheke versandapotheke versandkostenfrei
http://potenzmittel.men/# online apotheke deutschland
https://kamagrakaufen.top/# online apotheke gГјnstig
https://cialiskaufen.pro/# internet apotheke
I did HCG throughout cycle and according to the girl my vomiting dolphine was bigger, I was on 1000 a week, but it s smaller than ever now during PCT, so I bet it was just some extra bloodflow propecia cost When people consume unusual diets, e
https://potenzmittel.men/# versandapotheke versandkostenfrei
gГјnstige online apotheke potenzmittel online apotheke versandkostenfrei
gГјnstige online apotheke internet apotheke versandapotheke versandkostenfrei
https://potenzmittel.men/# online apotheke preisvergleich
https://apotheke.company/# online apotheke gГјnstig
https://potenzmittel.men/# online apotheke deutschland
versandapotheke versandkostenfrei potenzmittel kaufen gГјnstige online apotheke
http://kamagrakaufen.top/# п»їonline apotheke
pharmacie ouverte 24/24 Levitra 20mg prix en pharmacie Pharmacie en ligne livraison gratuite
https://cialissansordonnance.pro/# acheter medicament a l etranger sans ordonnance
http://viagrasansordonnance.store/# Sildénafil Teva 100 mg acheter
farmacia online madrid: vardenafilo – farmacia barata
https://cialissansordonnance.pro/# pharmacie en ligne
Meilleur Viagra sans ordonnance 24h: Meilleur Viagra sans ordonnance 24h – Quand une femme prend du Viagra homme