About the use of defining an empty array at the end of a structure body

Source: Internet
Author: User
About defining the use of empty arrays at the end of a structure, I've seen it before, and I've never understood it, and I've been accidentally aiming at the answer, it's privilege, O ' Dover.


So put it all together and share it:


struct Ast_exten {
Char *exten; /* Extension Name * *
int matchcid; /* Match Caller ID? */
Char *cidmatch; /* Caller ID to match for this extension * *
int priority; * Priority * *
Char *label; /* Label * *
struct Ast_context *parent; /* The context this is extension belongs to/*
Char *app; /* Application to execute * *
void *data; /* Data to use (arguments) * *
void (*datad) (void *); /* Data destructor * *
struct Ast_exten *peer; /* Next higher priority with our extension *
const char *registrar; * Registrar * *
struct Ast_exten *next; /* Extension with a greater ID * *
Char stuff[0];
};


Another example:
struct Ast_include {
Char *name;
Char *rname; /* context to include/*
const char *registrar; * Registrar * *
int hastime; /If time construct exists * *
struct ast_timing timing; /* Time Construct * *
struct Ast_include *next; /* Link them together * *
Char stuff[0];
};


struct Ast_ignorepat {
const char *registrar;
struct Ast_ignorepat *next;
Char pattern[0];
};


This is a widely used common technique used to form buffers. Using an empty array has the advantage over pointers:
1. Do not need initialization, the array name is directly the offset
2. No space, the pointer needs to occupy int length space, empty array does not occupy any space.


"This array does not occupy any memory" means that such a structure saves space; "The memory address of the array is the same as the address of the element behind it," meaning that without initialization, the array name is the address of the later element and can be used directly as a pointer.


This is the most suitable way to make dynamic buffer. Because you can allocate space like this:
malloc (sizeof (struct XXX) + Buff_len);
It's not good to see. The structure of the buffer and the buffers are allocated directly. It is also very convenient to use, because now Kongsuzu actually become an array of buff_len length.
The benefits are:
One allocation solves problems and saves a lot of trouble. As you know, in order to prevent memory leaks, if the partition is divided two times (the structure and buffer), then if the second malloc fails, you must roll back the first allocated structure. This brings the coding trouble. Second, after assigning a second buffer, assign a value to the pointer if the structure is using a pointer. Similarly, in the free this buffer time, uses the pointer also to need two times free. If you use an empty array, all problems are resolved once.
Second, we know that the management of small memory is very difficult, if with pointers, this buffer struct part is small memory, in the system there are many will seriously affect the performance of memory management. If you use an empty array to allocate the struct and the actual data buffer one block at a time, there is no problem.


So it seems that using an empty array simplifies the coding and solves the problem of small memory fragmentation and improves performance, he le. should be widely adopted.


If you have done the development of the operating system or embedded development, this kind of technique should be to be seen to be excessive of hehe.
---------------------------------------------------------------


This is what Newbiestar () extension flexible array member, which is allowed by C99. The standard text is as follows (117th page):


As a special case, the last element of a structure with more than one named
Have an incomplete array type; This is called a flexible array member. With two
Exceptions, the flexible array member is ignored. The size of the structure shall be
Equal to the offset of the last element of a otherwise identical structure that replaces the
Flexible array unspecified length.106) Second, when a. (or->)
Operator has a left operand that's (a pointer to) a structure with a flexible array member
And the right operand names so member, it behaves as if this member were replaced
With the longest array, with the same element type, that would is not make the structure
Larger than the object being accessed; The offset of the array shall remain that of the
Flexible array member, even if this would differ the replacement array. If this
Array would have no elements, it behaves as if it had one element but the behavior is
Undefined if any attempt be made to access this element or to generate a pointer one past

It.


Http://hellosq.blog.hexun.com/5248305_d.html


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.