The use and principle of an array of length 0 in a struct

Source: Internet
Author: User

Objective

In standard C and C + +, arrays with a length of 0 are forbidden. In Gnuc, however, there is a very strange usage, that is, an array of length 0, such as array[0]; Many people may find it inconceivable that an array of length 0 is meaningless, but here it is entirely another layer of meaning that is not portable, so if you are committed to writing portable or slightly cross-platform code, these trick are best to be collected.

This series of articles are written by the author, inevitably there are some errors or flaws, if the small partners have good suggestions or better algorithms, please advise.

Body

in the GNU Guide, it is so written:

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

This usage is mainly used to change the size of the Buffer,structline 4, the structure of the contents[0] does not occupy any space, even a pointer to the space is not accounted for, contents here just represents a constant pointer, this feature is implemented by the compiler , that is, when using thisline->contents, this pointer represents a buffer in the allocated memory address, such as malloc (struct line) +this_length return is 0X8F00A40, The location where thisline->contents points is (0x8f00a40+ sizeof), where sizeof (struct line) is just four bytes of an int.

for this usage, the struct pointer we define can point to a memory buffer of any length, a technique that is quite handy when used in a variable-length buffer. A friend might say, why not just define the last contents as a pointer? The difference here is that if it is defined as a pointer, it needs to occupy 4Bytes, and it must be artificially assigned after the memory is applied. If you use this usage, this constant pointer does not occupy space and does not require a value assignment. However, the convenience is not absolute, when releasing the allocated memory, because the function free will think that *thisline just point to a 4-byte pointer, that will only release the length of space, and for the back of the big head of the buffer is blind, this requires human intervention Instead, the allocated memory can be freed directly with free (thisline->contents) for the subsequent declaration of pointers.

ASSERT: Unless necessary, do not use this feature easily, GNUC can be compiled through, so you are using VC + +, then do not have to try, compile can not pass.

The use and principle of an array of length 0 in a struct

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.