Linux C柔性数组(零长数组)

发布于:2024-04-14 ⋅ 阅读:(25) ⋅ 点赞:(0)

        零长数组,大小为0,一般用在结构体中(网络通信,省流),节省空间,方便善后(相对于指针类型),我们通过具体例子进行理解。

常规定长数组

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define CUR_LEN 512
#define MAX_LEN 1024

typedef struct
{
    int len;
    char data[MAX_LEN];
}St_FixedSize;

int main()
{
	St_FixedSize *fixed_size = NULL;
	//数组大小
	printf("the size of St_FixedSize:%d\n", sizeof(St_FixedSize));
	
	//申请空间并赋值
	if((fixed_size = (St_FixedSize *)malloc(sizeof(St_FixedSize))) != NULL)
	{
	    fixed_size->len = CUR_LEN;
	    memcpy(fixed_size->data, "Hello World", CUR_LEN);
	    printf("%d, %s\n", fixed_size->len, fixed_size->data);
	}
	
	//销毁
	free(fixed_size);
	fixed_size = NULL;
	
	return 0;
}

执行结果:

此结构体大小 = sizeof(int) + sizeof(char*MAX_LEN) = 4 + 1024 = 1028,但是data我们只使用了12个字节,浪费了1024 - 12 = 1012个字节的空间,缺点暴露无遗,优点是只申请了一次内存空间。

常规指针数组

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define CUR_LEN 512
#define MAX_LEN 1024

typedef struct
{
    int len;
    char *data;
}St_PointSize;

int main()
{
	St_PointSize *point_size = NULL;
	//结构体大小
	printf("the size of St_PointSize:%d\n", sizeof(St_PointSize));
	
	//申请空间并赋值
	if((point_size = (St_PointSize *)malloc(sizeof(St_PointSize))) != NULL)
	{
	    point_size->len = CUR_LEN;
		point_size->data = (char *)malloc(CUR_LEN);
		if(point_size->data != NULL)
		{
			memcpy(point_size->data, "Hello World", CUR_LEN);
	    	printf("%d, %s\n", point_size->len, point_size->data);
		}
	    
	}
	
	//销毁
    if(point_size->data)
    {
        free(point_size->data);
    }
	
	free(point_size);
	point_size = NULL;
	
	return 0;
}

执行结果为:

此结构体大小 = sizeof(int) + sizeof(不同平台不同编译器下的指针大小) = 16,空间的确节省了,但是在申请和释放的阶段都进行了两次,比较麻烦,容易引起内存泄漏问题。

柔性数组

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define CUR_LEN 512
#define MAX_LEN 1024

typedef struct
{
    int len;
    char data[0];
}St_OSize;

int main()
{
	St_OSize *Osize = NULL;
	//结构体大小
	printf("the size of St_PointSize:%d\n", sizeof(St_OSize));
	
	//申请空间并赋值
	if((Osize = (St_OSize *)malloc(sizeof(St_OSize) + CUR_LEN)) != NULL)
	{
	    Osize->len = CUR_LEN;
		memcpy(Osize->data, "Hello World", CUR_LEN);
	    printf("%d, %s\n", Osize->len, Osize->data);
	    
	}
	
	//销毁
	free(Osize);
	Osize = NULL;
	
	return 0;
}

执行结果:

结构体大小只有成员变量int len的大小,而data变量不占空间(char data[0]不同于指针),优点很明显,只申请了一次空间,也只释放了一次空间,同时也满足了节省空间的需求,另外这个结构体的空间还是连续的(大家可以打印下相应字段的地址)。

但是,柔性数组只能被>=C90的编译器支持(char data[]写法被C99支持)且并不没有被包含近C/C++的标准之中(GNU C中是允许的)。大家根据实际需要灵活使用吧。

参考

C语言0长度数组之《可变数组/柔性数组》详解 - 知乎