Svoboda | Graniru | BBC Russia | Golosameriki | Facebook

To install click the Add extension button. That's it.

The source code for the WIKI 2 extension is being checked by specialists of the Mozilla Foundation, Google, and Apple. You could also do it yourself at any point in time.

4,5
Kelly Slayton
Congratulations on this excellent venture… what a great idea!
Alexander Grigorievskiy
I use WIKI 2 every day and almost forgot how the original Wikipedia looks like.
Live Statistics
English Articles
Improved in 24 Hours
Added in 24 Hours
Languages
Recent
Show all languages
What we do. Every page goes through several hundred of perfecting techniques; in live mode. Quite the same Wikipedia. Just better.
.
Leo
Newton
Brights
Milds

Flexible array member

From Wikipedia, the free encyclopedia

C struct data types may end with a flexible array member[1] with no specified size:

struct vectord {
    short len;    // there must be at least one other data member
    double arr[]; // the flexible array member must be last
    // The compiler may reserve extra padding space here, like it can between struct members
};

Typically, such structures serve as the header in a larger, variable memory allocation:

struct vectord *vector = malloc(...);
vector->len = ...;
for (int i = 0; i < vector->len; i++)
     vector->arr[i] = ...;  // transparently uses the right type (double)

YouTube Encyclopedic

  • 1/3
    Views:
    389
    96 410
    20 205
  • F05 / 6: 9.3.1.3 flexible array member (level 3)
  • 9.5: Arrays of Flexible Size - Processing Tutorial
  • Fixed and Variable Length Arrays in C and C++

Transcription

Effect on struct size and padding

The sizeof operator on such a struct gives the size of the structure as if the flexible array member were empty. This may include padding added to accommodate the flexible member; the compiler is also free to re-use such padding as part of the array itself.[2]

It is common to allocate sizeof(struct) + array_len*sizeof(array element) bytes.

This is not wrong, but it may allocate a few more bytes than necessary: the compiler may be re-purposing some of the padding that is included in sizeof(struct). Should this be a concern, macros are available[3] to compute the minimum size while ensuring that the compiler's padding is not disrupted.

As the array may start in the padding before the end of the structure, its content should always be accessed via indexing (arr[i]) or offsetof, not sizeof.

Availability

Flexible array members were officially standardized in C99.[4] In practice, compilers (e.g., GCC,[5]MSVC[6]) provided them well before C99 was standardized.

Flexible array members are not officially part of C++, but language extensions[7] are widely available.

References

  1. ^ "Lesser known C features". Retrieved December 30, 2014.
  2. ^ "flexible array member". Jens Gustedt's Blog. March 14, 2011. Retrieved October 9, 2018.
  3. ^ "P99: Flexible array members". p99.gforge.inria.fr. Retrieved October 9, 2018.
  4. ^ C99 section §6.7.2.1, item 16, page 103, http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf
  5. ^ "Zero Length - Using the GNU Compiler Collection (GCC)". Retrieved December 30, 2014.
  6. ^ "Structure Declarations". Microsoft. Retrieved April 25, 2020.
  7. ^ E.g., "Arrays (C++)". Microsoft. Retrieved April 25, 2020. A zero-sized array is legal only when the array is the last field in a struct or union and when the Microsoft extensions (/Ze) are enabled.
This page was last edited on 1 January 2024, at 16:10
Basis of this page is in Wikipedia. Text is available under the CC BY-SA 3.0 Unported License. Non-text media are available under their specified licenses. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc. WIKI 2 is an independent company and has no affiliation with Wikimedia Foundation.