C language flexible array

The concept of flexible array

The flexible array (flexible array member) also called stretchable array members, to seek such a configuration produces dynamic structure. In daily programming, may need to be stored in the structure of a dynamic length string (and possibly other types of data), in general practice, it is a pointer to the structure member is defined, the pointer points to a member of the string is located dynamic memory space.

Under normal circumstances, if you want an efficient use of memory, static within the definition of the structure of the array is very wasteful behavior. In fact, the idea and the idea of ​​a flexible array of dynamic arrays is the same.

The flexible array used to store the length of a string in the dynamic structure.

Common way

In fact, do not flexible array like us can do: define a method in the structure, in the method dynamically pointer to dynamic array

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

typedef struct Test
{
    int a;
    char *p;
} Test;

int main()
{
    char *str = (char *)malloc(sizeof(char) * 10);
    strcpy(str, "hello");
    Test *t = (Test *)malloc(sizeof(Test));
    t->p = str;

    printf("%s\n" , (T-> P)); 
    the printf ( " the Address: \ n- " ); 
    the printf ( " T \ T% P \ n- " , T); 
    the printf ( " TA \ T% P \ n- " , & (T -> a)); 
    the printf ( " TP \ T% p \ n- " , (T-> p));
     Free (T);
     Free (T-> p);     // also need to explicitly referred to the release of p memory 
    return  0 ; 
}

operation result:

fc7948c3-687d-4d9c-a13a-289c840dc53b

The above code is indeed possible to complete the results we want. We looked at the start address pointer p and arrays. We can see the memory of the memory block and string dynamic array is two different memory.

However, when we free up space, you need to explicitly free memory space referenced by pointer p, or else there will be a memory leak situation.

A flexible manner array

From the beginning C99 will support an incomplete type implements a flexible array members. Why is it incomplete type?

int a[] = {10};

See this declaration, we found a [] array is actually a sign, incomplete type, due to the assignment, so at compile time will determine the size of the array, it is a complete array type.
In the structure will be specified at runtime dynamic array using the incomplete type.

C99 standard is defined as follows

struct the Test {
     int A;
     char P []; // not only char type, other types may likewise 
}

Because of the continuity of the statement memory, flexible array members must be defined in the final structure, and can not be the only member.
Let's take a look at the entire structure (including the distribution of the memory array)

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

typedef struct Test
{
    int a;
    char p[];
} Test;

int main()
{
    Test *t=(Test*)malloc(sizeof(Test)+sizeof(char)*(10+1));
    strcpy(t->p,"hello");
    printf("%s\n", (t->p));
    printf("Address:\n"); 
    The printf ( " T \ T% P \ n- " , T); 
    the printf ( " TA \ T% P \ n- " , & (T-> A)); 
    the printf ( " TP \ T% P \ n- " , (T-> P));
     Free (T);     // only released once memory 
    return  0 ; 
}

Then run the results:

7ee88005-9523-4181-afeb-7c4f29c0c017 [4]

Operation can be seen from the results, the entire structure is continuous, and the release structure is also very simple manner directly on the structure pointer to be released.

[Note] This is because the C99 standard, ISO C is not allowed in the specification and in C ++. Use 0 vs length in the array may get a warning.
However gcc, clang ++ support in advance of the C99 games are played, so in Linux compiler without warning

Further understanding of flexible array

An existing program

#include<stdio.h>
typedef struct _SoftArray{
    int len;
    int array[];
}SoftArray;

int main()
{
    int len = 10;

    printf("The struct's size is %d\n",sizeof(SoftArray));
}

operation result:

dc83b57f-2795-4d1b-9c2f-8b3655f0ba19

We can see that the size of the structure is 4 _SoftArray, apparently, in a 32-bit operating system just int variable size 4, also said that the structure of the array does not take up memory. Why not take up memory, we usually have clearly indicated from time to time with an array of array size of it? But here you can compile it? This is what we often say that the dynamic array, which is a flexible array.

Do not mess, let us look at a piece of code

#include <stdio.h>
#include <malloc.h>

typedef struct _SoftArray
{
    int len;
    int array[];
} SoftArray;

int main()
{
    int len = 10;

    SoftArray *p = (SoftArray *)malloc(sizeof(SoftArray) + sizeof(int) * len);
    printf("After the malloc function the struct's size is %d\n",sizeof(SoftArray));

    return 0;
}

operation result:

6c5f8aed-22f2-4852-a3fc-4ca5aa1b8356

Is not it a bit strange, why apply a structure the size of the memory or 4 it?

The reason: a dynamic memory apply only to the application used to expand the array, from the last program we can see that the size of the structure has been determined at the time of creation, array is not a clear structure members, just trying to sell it .

Let us look at an array of information on flexible:

1. What is a flexible array?

The flexible array of pending array of both array size, the last element of the C language structure can be an array of unknown size, also known as zero length, so we can use the structure to create a flexible array.

2, flexible array of any use?

Its main purpose is to meet the need for a variable length structure, in order to solve the problem of cross-border memory using redundant arrays and arrays.

3, use : in a final structure, an array of length affirmed empty, so that the structure can be of a variable length. To the compiler, then the length of the array is not 0 space, because the array name

Not the space itself, it's just an offset to the symbol array name itself represents a non-modifiable address constants (Note:! Array name will never be a pointer), but for the size of the array, we can dynamically allocation, to the compiler, the name of the array is just a symbol, it does not take up any space, it is in the structure, only represents an offset, on behalf of a non-modifiable address constant!

For this feature a flexible array becomes easy to construct the structure, such as buffers, and the like packet:

typedef struct _SoftArray
{
    Int len;
    int array[];
}SoftArray;

The use of the flexible array

    Such a variable-length array configuration used in the communication network of variable length packets, does not waste space wasted network traffic, for example, I want to send 1024 bytes of data, if the fixed-length packets, fixed-length packet is assumed to 2048, 1024 bytes would be a waste of space, waste can also cause unnecessary traffic.

    In fact, it is particularly flexible array members usage table when leaps in Redis the SDS data structure and leaps on the tables, use a flexible array members.

Guess you like

Origin www.cnblogs.com/WindSun/p/11286141.html