Flexible array----dynamic memory allocation

Hello, everyone, today I am sharing a flexible array that is not commonly used in the C language. Although we do not use flexible arrays very much, it has some advantages, such as being used with several functions of our dynamic memory opening. The space utilization efficiency is very high, so today we will talk about it.

flexible array

1. There must be an element in front of the flexible array

2 Flexible array sizeof calculation does not include the size of the flexible array

3. When we use malloc, we need to open up space for it in advance.

So let's first look at a flexible array and see what it looks like


#include<stdio.h>
int main()
{
    
    
	struct S
	{
    
    
		int i;
		char c;
		int arr[];
	};
	return 0;
}

Of course we can also write it like this


#include<stdio.h>
int main()
{
    
    
	struct S
	{
    
    
		int i;
		char c;
		int arr[0];
	};
	return 0;
}

However, it cannot be implemented on some platforms under some compilers. We only need to remove its size. Let's calculate its size again.


#include<stdio.h>

int main()
{
    
    
	struct S
	{
    
    
		int i;
		char c;
		int arr[];
	};
	printf("%zd", sizeof(struct S));
	return 0;
}

Through our compilation, we can see that the result is 8 bytes in size, which means that it does not include the size of the flexible array, so we only need eight bytes in size when calculating.

Now that we’ve talked about flexible arrays, let’s take a look at its usage. We said that it can be used together with our dynamic memory allocation, so now we need to open up space for this structure. When opening, we also need to set the size of the flexible array. What does it mean to open up new space in advance? It means that if the size of our structure is, for example, eight bytes, then we must open up a word larger than eight bytes, and add an integer multiple of 4 to this. Section, of course, if it is other types such as char, it can be opened casually. Let us first assume that the opened array is an integer array.

#include<stdio.h>
#include<stdlib.h>
int main()
{
    
    
	struct S
	{
    
    
		int i;
		char c;
		int arr[];
	};
	//printf("%zd", sizeof(struct S));
	struct S* ptr = (struct S*)malloc(sizeof(struct S) + 20);
	if (ptr == NULL)
	{
    
    
		perror("malloc fail\n");
		exit(-1);
	}
	ptr->i = 4;
	ptr->c = 'w';
	//用柔性数组
	int i = 0;
	for (i = 0; i < 5; i++)
	{
    
    
		ptr->arr[i] = i;
	}
	for (i = 0; i < 5; i++)
	{
    
    
		printf("%d ",ptr->arr[i]);
	}

	return 0;
}

We are simply using it here. We said before that it is highly efficient. Why is this? The reason is that the space we open up is continuous. In fact, our position is a structure, but if we are not a structure, we The use of space is fragmented, because our operating system will allocate a space for us to use. If we use a flexible array, there will be no waste of space when using it, but the efficiency here is not improved much.

After our dynamic memory is used, we will return it to the operating system, so the next step is free

#include<stdio.h>
#include<stdlib.h>
int main()
{
    
    
	struct S
	{
    
    
		int i;
		char c;
		int arr[];
	};
	//printf("%zd", sizeof(struct S));
	struct S* ptr = (struct S*)malloc(sizeof(struct S) + 20);
	if (ptr == NULL)
	{
    
    
		perror("malloc fail\n");
		exit(-1);
	}
	ptr->i = 4;
	ptr->c = 'w';
	//用柔性数组
	int i = 0;
	for (i = 0; i < 5; i++)
	{
    
    
		ptr->arr[i] = i;
	}
	for (i = 0; i < 5; i++)
	{
    
    
		printf("%d ",ptr->arr[i]);
	}
	free(ptr);
	ptr = NULL;

	return 0;
}

Speaking of which, we can actually use a dynamic function to simulate one, that is, malloc another one to store the array, but this is separate, so there is a problem of our space usage efficiency.

That’s it for the sharing of C language. See you on C++ Linux.

Guess you like

Origin blog.csdn.net/2301_76895050/article/details/133364196