Take you to understand flexible array!!!!!

Keywords: C

What is a flexible array

You may have never heard of the concept of flexible arrays, but it does exist.
In the C99 standard, the last element in the structure is allowed to be an array of unknown size, which is called a flexible array member

  • for example
struct st_type
{
	int i;
	int a[];//Flexible array member
};

It can also be written as

struct st_type
{
	int i;
	int a[0];//Flexible array member
};
  • The above two forms are correct, but only one of them may be used in different compilers, so if one writing method doesn't work, change the other

Characteristics of flexible array

  • A flexible array member in a structure must be preceded by at least one other member.
  • The structure size returned by sizeof does not include the memory of the flexible array.
  • For structures containing flexible array members, malloc() is used for dynamic memory allocation, and the allocated memory should be larger than the size of the structure to adapt to the expected size of the flexible array.
  • for example
struct st_type
{
	int i;
	int a[0];//Flexible array member
};
printf("%d\n", sizeof(st_type));//The output is 4

Use flexible arrays

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

struct st_type
{
	int i;
	int a[];//Flexible array member
};
int main()
{
	//Open up a flexible array with an array size of 10
	struct st_type* ps = (struct st_type*)malloc(sizeof(struct st_type) + 10 * sizeof(int));
	//Assign values to these spaces
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		ps->a[i] = i;
	}
	//Print the contents of the array
	for (i = 0; i < 10; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
	//Increase space size
	struct st_type* ptr = (struct st_type*)realloc(ps, sizeof(struct st_type) + 20 * sizeof(int));

	if (ptr != NULL)//Judge whether the development of space is successful
	{
		ps = ptr;
	}

	for (i = 10; i < 20; i++)
	{
		ps->a[i] = i;
	}
	//Print the contents of the array
	for (i = 0; i < 20; i++)
	{
		printf("%d ", ps->a[i]);
	}
	//release
	free(ps);
	ps = NULL;
	return 0;
}
  • So can we complete the function of appeal code without flexible array?
  • Of course, the answer is yes. Put the code directly
#include <stdio.h>
#include <stdlib.h>

struct st_type
{
	int i;
	int* a;
};

int main()
{
	struct st_type* ps = (struct st_type*)malloc(sizeof(struct st_type));
	ps->a = (int*)malloc(10 * sizeof(int));//Open up array space of size 10
	int i = 0;
	//Assign values to these spaces
	for (i = 0; i < 10; i++)
	{
		ps->a[i] = i;
	}
	//Print
	for (i = 0; i < 10; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
	//Resize a
	int* ptr = (int*)realloc(ps->a, 20 * sizeof(int));
	if (ptr != NULL)
		ps->a = ptr;
	//Assign values to these spaces
	for (i = 10; i < 20; i++)
	{
		ps->a[i] = i;
	}
	//Print
	for (i = 0; i < 20; i++)
	{
		printf("%d ", ps->a[i]);
	}
	//release
	free(ps->a);
	ps->a = NULL;
	free(ps);
	ps = NULL;
	return 0;
}

Benefits of flexible arrays

  • Since flexible arrays can be replaced by other methods, why add the concept of flexible arrays to C99?
  • Then we need to mention the advantages of flexible arrays

First advantage

Easy to free memory

  • If our code is in a function for others, you make a secondary memory allocation in it and return the whole structure to the user. The user can release the structure by calling free, but the user does not know that the members in the structure also need free, so you can't expect the user to find it. Therefore, if we allocate the memory of the structure and its members at one time and return a structure pointer to the user, the user can free all the memory once.

Second advantage

This is conducive to access speed

  • Continuous memory is beneficial to improve access speed and reduce memory fragmentation.

Posted by kishanprasad on Sat, 02 Oct 2021 11:19:53 -0700