An array of length 0 in C + +

Source: Internet
Author: User

Reprinted from Http://www.cnblogs.com/tangxin-blog/p/5560699.html

Reference: http://blog.csdn.net/zhaqiwen/article/details/7904515

Recently, when looking at the framework code in the project, I found a strange syntax: An array of length 0, for example

uint8_t buf[0];

I have never seen such a writing, so I check the information on the Internet, understand and record it.

An array of length 0 is not allowed in standard C/s + +, which is a C/s + + extension, and you can use it if your compiler supports this extension.

The VS Series compiler does not fully support this extension, and if you do, you will probably get a warning at compile time: Warning C4200: Using nonstandard extensions: 0-size arrays in structs/unions, when a UDT contains an array of size zero, the copy constructor or copy assignment operator cannot be generated

The gun compiler fully supports this extension, and you can legitimately declare an array of length 0, but the most typical usage of such a declaration is the last item in the array, in order to facilitate the management of memory buffers, for example:

struct line{    uint32_t length;    Char contents[0];};

In the struct, an array of length 0 does not occupy storage space, in the above example sizeof (line) =4

When the memory space is requested, the buffer space can be applied together with the structure's space, and one operation can be completed. For example,

uint32_t length = 10;struct line *pline = [struct line *] malloc (sizeof (struct line) + length);p line->length = length;

The code above dynamically applies a buffer of length 10byte for the struct, and because it is the same malloc operation, the memory address of the buffer and struct is contiguous, and the buffer element can be accessed by array subscript, for example

for (uint32_t i = 0;i < Pline->length;++i) {    pline->contents[i] = i;}

Since the memory address of the buffer and the struct is contiguous, only one free operation is required when the memory is freed.

In summary, using an array of length 0 has the following advantages over defining a pointer to another buffer address in the struct:

The 1-> pointer itself takes up memory, and an array of length 0 does not require

An array of length 0 defines a buffer that can be in the same contiguous address as the struct, as long as the malloc operation and the free operation are 2->. If you use pointers, you need to request and release the memory blocks of the structure body and pointer, at least two times more memory operations.

Test code:

Compiler: GCC version 4.8.1 (tdm-2)

#include <stdio.h> #include <stdint.h> #include <malloc.h>struct line{    uint32_t length;    uint8_t contents[0];}; int32_t Main () {    uint32_t length = ten, I;    printf ("sizeof (line) =%d\n", sizeof (struct line));    struct line *pline = [struct line *] malloc (sizeof (struct line) + length);    pline->length = length;    for (i = 0; i < pline->length; ++i)    {        pline->contents[i] = i;    }    for (i = 0; i < pline->length; ++i)    {        printf ("i=%d,contents[i]=%d\n", I, Pline->contents[i]);    }    //free (pLine);    return 0;}

Successful execution and printing of the results:

sizeof (line) =4i=0,contents[i]=0i=1,contents[i]=1i=2,contents[i]=2i=3,contents[i]=3i=4,contents[i]=4i=5, Contents[i]=5i=6,contents[i]=6i=7,contents[i]=7i=8,contents[i]=8i=9,contents[i]=9press any key to continue ...

Compile and run the above code in VS2013, the program can run correctly in addition to the warning "warning C4200".

An array of length 0 in C + +

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.