ok, it's a flexible array

content

1. Features of flexible arrays

2. The use of flexible arrays

1. How to use flexible arrays

2. What is the alternative if you don't need a flexible array?

3. Advantages of flexible arrays

1. Features of flexible arrays

        

struct S
{
	int x;
	int a[];
};
int main()
{
	printf("%d", sizeof(S));
}

What is the output of this code?

We print the size of the space occupied by the structure S. How many bytes does this a[] occupy?

The output result is 4, but an x ​​of type int is 4. Where does a[] go? So weird.

It turns out that this is a flexible array .

1. The last element in the structure is allowed to be an array of unknown size , which is a flexible array .

2. A flexible array in a structure must be preceded by at least one other member.

3. The size of this structure returned by sizeof does not include the memory of the flexible array.

4. The structure containing the members of the flexible array uses the malloc function to dynamically allocate memory , and the allocated memory should be larger than the size of the structure to accommodate the expected size of the flexible array.

2. The use of flexible arrays

1. How to use flexible arrays

        The structure containing the members of the soft array uses the malloc function to dynamically allocate memory ,

        And the allocated memory should be larger than the size of the structure to fit the expected size of the flexible array.

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>

struct S
{
	int x;
	int a[];
};
int main()
{
    //为柔性数组a[]开辟了40个字节的空间
	struct S *ps =(struct S*)malloc(sizeof(struct S) + 40);
	if (ps == NULL)                                    //检查是否为空指针
	{
		perror("ps");
		return 1;
	}
	ps->x = 10;												
	int i = 0;					
	for (i = 0; i < 10; i++)								
	{                                                  
		ps->a[i] = i;                                   //数组使用
	}
	for (i = 0; i < 10; i++)
	{
		printf("%d  ",ps->a[i]);                        //数组打印
	}
	//若觉得40不够用,可用realloc扩容
	//如:
	struct S* ptr = (struct S*)realloc(ps, sizeof(struct S) + 80);
	if (ptr == NULL)                                    //检查是否为空指针
	{
		perror("realloc");
		return 1;
	}
	else
	{
		ps = ptr;
	}

	free(ps);                                            //释放内存并置为空指针
	ps = NULL;
}

2. What is the alternative if you don't need a flexible array?

We often use string pointers to allocate space,

Then we directly malloc a space for the string pointer, isn't it alright?

Why use flexible arrays?

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>

struct S
{
	int x;
	int *a;
};
int main()
{
	struct S* ps = (struct S*)malloc(sizeof(struct S) );//为结构体变量x开辟空间
	if (ps == NULL)
	{
		return 1;
	}
	ps->a = (int*)malloc(40);							//为字符串指针开辟40个字节的空间
	if (ps->a == NULL)
	{
		free(ps);
		ps = NULL;
		return 1;
	}
	free(ps->a);
	ps->a = NULL;

	free(ps);
	ps = NULL;
}

The above code does exactly the same thing,

But flexible arrays are relatively better.

Let's take a look at the advantages of flexible arrays .

3. Advantages of flexible arrays

1. Convenient memory release

        With flexible arrays we only use free once,

        However, using a string pointer requires freeing twice, and there is a risk of memory leaks.

2. Improve access speed

        The flexible array is malloced once and is contiguous memory ,

        Beneficial to improve access speed and reduce memory fragmentation.

        

Guess you like

Origin blog.csdn.net/m0_63742310/article/details/123836726