Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

// request.cf · coarse context

A page that knows where it met you.

Only coarse request metadata is shown. This demo does not display or persist visitor IP addresses.

Country
US
Cloudflare location
CMH
Connection
HTTP/2
Language
Not provided

Ray ID: a22c74493de61210

Jump to content

// Workers AI · dad joke modeWhat did the flexible array member say? I'm flexible.

From Wikipedia, the free encyclopedia

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

typedef struct {
    size_t 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
} DoubleArray;

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

// see section on size and padding
DoubleArray* darray = malloc(sizeof(DoubleArray) + ... * sizeof(double));
darray->len = ...;
for (int i = 0; i < darray->len; i++) {
    darray->arr[i] = ...; // transparently uses the right type (double)
}

Effect on struct size and padding

[edit]

The sizeof operator on such a struct gives the size of the struct as if the flexible array member were not present.[2] This may include padding added to accommodate the flexible member; the compiler is also free to reuse such padding as part of the array itself.[3]

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[4] 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.[5]

History

[edit]

Flexible array members were officially standardized in C99.[6] In practice, compilers (e.g., GCC,[7] MSVC[8]) provided them well before C99 was standardized. Widely employed prior to the standardization of flexible array members, the use of such extensions was known as the struct hack.[1][9] The term "struct hack" is also used to refer to the use of a standard array member of length 1 as if it were a flexible array member.[10][11][12]

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

References

[edit]
  1. 1 2 Azebu, E.; Ballman, A.; Britton, J.; Bubnik, V.; Campbell, G. A.; Clare, G.; Flynn, L.; Gale, A.; Hicken, A.; Keaton, D.; Klieber, W.; Kubo, M.; Lallier, C.; Long, F.; Marjamäki, D.; Seacord, R.; Sebor, M.; Shrum, S.; Snavely, W.; Svoboda, D.; Toda, Y.; White, B.; Whiting, L. (June 29, 2016). "3.5 DCL38-C. Use the correct syntax when declaring a flexible array member". SEI CERT C Coding Standard (PDF) (2016 ed.). Software Engineering Institute. pp. 50–52. Archived from the original (PDF) on April 2, 2018.
  2. 1 2 Seacord, Robert C. (2020). "Flexible Array Members". Effective C: An Introduction to Professional C Programming. No Starch Press. pp. 110–111. ISBN 978-171850104-1.
  3. Gustedt, Jens (March 14, 2011). "flexible array member". Jens Gustedt's Blog. Retrieved October 9, 2018.
  4. Gustedt, Jens. "P99: Flexible array members". p99.gforge.inria.fr. Retrieved October 9, 2018.
  5. Gustedt, Jens. "13.1.3. Flexible array members". Modern C. Manning. p. 205. ISBN 978-161729581-2.
  6. C99 section §6.7.2.1, item 16, page 103, https://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf
  7. "Zero Length - Using the GNU Compiler Collection (GCC)". Retrieved December 30, 2014.
  8. Whitney, Tyler (November 4, 2016). "Structure Declarations". Microsoft Learn. Microsoft. Retrieved April 25, 2020.
  9. Prata, Stephen (2013). "Structures and Other Data Forms". C Primer Plus (6th ed.). Addison–Wesley. p. 636. ISBN 978-0-133-43238-1.
  10. ISO/IEC JTC 1 (April 2003). "Rationale for International Standard—Programming Languages—C" (PDF). Open Standards (5.10 ed.).
  11. Han, A. (2018). The Design and Implementation of an Adaptive Log Cache (Master of Science thesis). Santa Clara University. p. 16. S2CID 192648137. Wikidata Q140445459.
  12. Qureshi, A. (2023). C Notes for Professionals. Concepts Books. p. 83f. ISBN 979-83-864-1648-5.
  13. 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.