The purpose and principle of an array with a length of 0 in struct, struct Array

Source: Internet
Author: User

The purpose and principle of an array with a length of 0 in struct, struct Array
Preface

In standard C and C ++, an array with a length of 0 is forbidden. However, in GNUC, there is a very strange usage, that is, an Array with a length of 0, such as Array [0]. Many people may think this is incredible, an array with a length of 0 is meaningless, but here it represents another layer of meaning. This feature cannot be transplanted, if you are committed to writing portable code or a little cross-platform code, it is better to collect these Trick codes.

This series of articles are written by the author. It is inevitable that there will be some errors or omissions. If you have good suggestions or better algorithms, please do not hesitate to give us some advice.

Body

In the GNU guide, it writes:

struct line {int length;char contents[0];};//...ommit code here{struct line *thisline= (struct line *) malloc (sizeof (struct line) +this_length);thisline->length = this_length;}

This method is mainly used for variable-length Buffer. The structline size is 4. contents [0] In the struct does not occupy any space, or even the space of a pointer, contents only represents a constant pointer. This feature is implemented by the compiler. That is, when thisline-> contents is used, this pointer indicates a buffer in the allocated memory address, for example, malloc (sizeof (struct line) + this_length) returns 0x8f00a40, and thisline-> contents points to (0x8f00a40 + sizeof (struct line), and here sizeof (struct line) it is only four bytes of an int.

For this usage, we define a struct pointer that can point to a memory buffer of any length. This technique is quite convenient to use in a variable-length buffer. Some may say, why not define the final contents as a pointer? The difference here is that if it is defined as a pointer, it needs to occupy 4 Bytes and the address must be assigned after the memory is applied. If this function is used, the constant pointer does not occupy space and does not need to be assigned a value. However, convenience is not absolute. When the allocated memory is released, because function free considers * thisline only points to a 4-byte pointer, that is, only the length space is released, however, if you turn a blind eye to the buffer that occupies the largest part in the future, manual intervention is required. For the method of declaring pointer in the future, you can directly use Free (thisline-> contents) release the allocated memory.

ASSERT: Unless necessary, do not use this function easily. It can be compiled in GNUC. Therefore, if you are using vc ++, you do not need to try it and compilation will fail.

Related Article

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.