Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

The benefits and limitations of flexible array members

September 29, 2022
Serge Guelton
Related topics:
CompilersC, C#, C++Security
Related products:
Red Hat Enterprise Linux

    Flexible array members (FAM) is an extension of C89 standardized in C99. This article discusses how flexible array members offer convenience and improve performance and how compiler implementations can generate complications.

    FAM makes it possible to declare a struct with a dynamic size while keeping a flat memory layout. This is a textbook example:

    struct fam {
    
         int size;
    
         double data[ ];
    
    };

    The data array starts empty and will be loaded later, perhaps many times. Presumably, the programmer uses the size member to hold the current number of elements and updates that variable with each change to the data size.

    Flexible array members vs. pointer implementation

    Flexible array members allow faster allocation, better locality, and solid code generation. The feature is an alternative to a more traditional declaration of data as a pointer:

    struct fam {
    
         int size;
    
         double *data;
    
    };

    With the pointer implementation, adding an array element requires an extra load initializing the structure on the heap. Each element added to the array requires two allocations for the object and its data member. The process results in fragmented memory between the object and the area pointed at by data.

    Standard flexible array member behavior

    The C99 standard, section 6.7.2.1.16, defines flexible array members. A struct with a flexible array member behaves in interesting ways.

    It is legal to access any index of fam::data, providing enough memory has been allocated:

    struct fam * f = malloc(sizeof(struct fam) + sizeof(double[n]));
    
    f - > size = n;

    The sizeof operator behaves as if the FAM had zero elements but accounts for the padding required to position it correctly. For instance, sizeof(struct {char c; float d[];} is unlikely to be equal to sizeof(char) because of the padding required to correctly position d.

    The assignment operator does not copy the flexible array member, which probably explains why that operator is not part of the C++ standard.

    This would be the end of this post if there were no nonconformant compiler extensions.

    Nonconforming compiler extensions

    Flexible array members are supported only by GCC and Clang in C89 and C++ as extensions. The extensions use alternate syntax, sometimes called a struct hack.

    struct fam_extension {
    
         int size;
    
         double data[0];
    
    };

    Alternatively, you can specify:

    struct fam_extension {
    
         int size;
    
         double data[1];
    
    };

    As it turns out, this syntax extended to any array size due to prior art, as suggested in the FreeBSD developers handbook, section 7.5.1.1.2 sockaddr:

    struct sockaddr {
    
    unsigned char sa_len; /* total length */
    
    sa_family_t sa_family; /* address family */
    
    char sa_data[14]; /* actually longer; address value */
    
    };

    Note that using an array size different from 0 for the FAM makes the allocation idiom more complex because one needs to subtract the size of the FAM:

    struct fam * f = malloc(sizeof(struct sockaddr) + sizeof(char[n]) - sizeof(char[14]));

    The GCC and Clang extensions normalize the undefined behavior when performing an out-of-bounds access on an array. The program performs regular memory access as if it allocated the memory.

    Limitations of sized arrays

    The ability to consider sized arrays as FAM impacts the accuracy of some kinds of code analysis. Consider, for instance, the -fsanitize=bounds option in which the instruments array detects when they are out-of-bounds. Without any context information, it cannot add a check to the following access:

    struct fam {
    
       int size;
    
       double data[];
    
    };
    
    int foo(struct fam* f) {  return f -> data[8]; }

    But if we declare the array as double data[1], there is still no instrumentation. The compiler detects a FAM based on the extension definition and performs no check. Even worse, if we declare the array as double data[4], trunk GCC performs no check (honoring legacy code, as illustrated in the previous section), while Clang adds a bounds check.

    We observe the same behavior for the __builtin_object_size builtin. This builtin computes the allocated memory reachable from a pointer. When asked for __builtin_object_size(f - > data, 1), both GCC and Clang return -1 (indicating a failure to compute that size) for all the declarations of data we have explored so far. This policy is conservative and removes some of the security offered by _FORTIFY_SOURCE, which relies heavily on the accuracy of __builtin_object_size.

    Motivation for stricter standard conformance

    A codebase that strictly conforms to the C99 standard (at least for FAM) would benefit from a compiler strictly following the standard definition of flexible array members. That goal motivates an effort currently led within the Linux kernel community, as demonstrated by this patch. The documentation update favors C99 FAM in place of zero-length arrays.

    To take advantage of this development, they developed a compiler option using GCC and Clang to give the programmer control over flexible array syntax. The option is -fstrict-flex-arrays= whereas:

    • 0 reflects the current situation described earlier.
    • 1 considers only [0], [1] and [ ] as a FAM.
    • 2 considers only [0] and [ ]as a FAM.

    Compiling code with a -fstrict-flex-arrays value greater than 0 unlocks some extra security while breaking (some) backward compatibility, which is why n=0 remains the default.

    Compiler convergence on C-language flexible array members

    Flexible array members is an interesting C99 feature that found its way, through compiler extensions, into C89 and C++. These extensions and legacy codes led to suboptimal code checks in the compiler, which the -fstrict-flex-arrays= option can now control.

    Last updated: August 14, 2023

    Related Posts

    • How C array sizes become part of the binary interface of a library

    • Detecting memory management bugs with GCC 11, Part 1: Understanding dynamic allocation

    • Customize the compilation process with Clang: Optimization options

    • How data layout affects memory performance

    Recent Posts

    • Protect data offloaded to GPU-accelerated environments with OpenShift sandboxed containers

    • Case study: Measuring energy efficiency on the x64 platform

    • How to prevent AI inference stack silent failures

    • Preventing GPU waste: A guide to JIT checkpointing with Kubeflow Trainer on OpenShift AI

    • How to manage TLS certificates used by OpenShift GitOps operator

    What’s up next?

    Getting GitOps e-book card

    Learn how to navigate the complex world of modern container-based software development and distribution with Getting GitOps: A Practical Platform with OpenShift, Argo CD, and Tekton.

    Get the e-book
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.