Questions tagged [flexible-array-member]

Flexible array members is a C feature introduced in C99 whereby one can declare the last element to be an array of unspecified size.

Quoted from N1570, 6.7.2.1 Structure and union specifiers:

18 As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply. However, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.

20 EXAMPLE 2 After the declaration:

      struct s { int n; double d[]; }; 

the structure struct s has a flexible array member d. A typical way to use this is:

      int m = /* some value */;
      struct s *p = malloc(sizeof (struct s) + sizeof (double [m]));

and assuming that the call to malloc succeeds, the object pointed to by p behaves, for most purposes, as if p had been declared as:

      struct { int n; double d[m]; } *p;

(there are circumstances in which this equivalence is broken; in particular, the offsets of member d might not be the same).

130 questions
5
votes
1 answer

How to partition and use heap memory allocated en masse with Rust?

I've read a lot on Rust lately but am still only beginning to oxidize. My brain retains most of its C/C++ reflexes so pardon me if this question is not relevant because of how things are done in Rust. Is it generally possible (desirable?) to…
dummydev
  • 423
  • 3
  • 14
5
votes
2 answers

C++ 'substitute' for structure with a flexible array member

Consider the following C99 structure, ending with a flexible array member: struct hdr { size_t len; size_t free; char buf[]; }; len, for example, gets accessed with an inline function (to be put into a header file) like this, having buf…
lemzwerg
  • 772
  • 7
  • 15
5
votes
2 answers

Unsized array declaration inside struct ok?

Is the following supposed to be valid for the declaration of table_type, specifically the e[]: struct table_type { unsigned int8 a; unsigned int8 b; unsigned int8 c; unsigned int8 d; unsigned int8 e[]; }; struct table_type table[]…
BasicPoke
  • 77
  • 1
  • 4
4
votes
2 answers

Flexible Array Member (Zero Length Array)

In reference to GCC's Zero Length Array explanation: This is particularly useful in the case when a struct is a header for a variable-length object. This is exactly my case. Furthermore, I am concerned with the alignment of my structs in the…
darksky
  • 20,411
  • 61
  • 165
  • 254
4
votes
2 answers

sizeof() a struct with a zero length array member

I'm confused about sizeof() output in C. Say I have: struct foo { char a; char b; char c; char d[0]; }; I would expect sizeof(struct foo) to be 4. However, it returns 3 after compiling with gcc. Also, when compiling the code with…
marcantonio
  • 958
  • 10
  • 24
4
votes
2 answers

Strict aliasing in flexible array member?

I'm writing an Arena Allocator and it works, but I feel like it violates strict aliasing rules. I want to know if I'm right or wrong. Here's the relevant part of the code: typedef struct ArenaNode ArenaNode; struct ArenaNode { ArenaNode…
wingerse
  • 3,670
  • 1
  • 29
  • 61
4
votes
2 answers

C++ object containing an array of char using unique_ptr

I am looking on a way to use unique_ptr to allocate a structure that contains an array of char with a number of bytes that set dynamically to support different types of message. Assuming: struct MyMessage { uint32_t id; uint32_t …
Less White
  • 561
  • 4
  • 13
4
votes
2 answers

Is it legal to implement inheritance in C by casting pointers between one struct that is a subset of another rather than first member?

Now I know I can implement inheritance by casting the pointer to a struct to the type of the first member of this struct. However, purely as a learning experience, I started wondering whether it is possible to implement inheritance in a slightly…
user4385532
4
votes
2 answers

flexible array member in a nested struct

Is it valid C code to have flexible array members inside nested structs? So is my sample code below guarenteed to work as expected with a sane compiler? #include #include struct d { char c; int ns[]; }; struct c { …
user3810155
4
votes
2 answers

flexible array in C and dereferencing type-punned pointer error

When I try to compile the code below with gcc -O3 -Wall -Werror -std=c99 main.c I get an error like "dereferencing type-punned pointer will break strict-aliasing rules" at #3, but not in #2 or #1. I can dereference type-punned "char *", but why…
ProgDevel
  • 77
  • 4
3
votes
2 answers

getting the following warning : The ABI of passing struct with a flexible array member has changed in GCC 4.4

When I try and run my program I get this warning and some weird bugs. rmi_pdu in the following structure contains a variable sized array which I want to access. struct rmi_message_s { /* Queue element containing Rmi message */ struct…
Nikhil
  • 2,168
  • 6
  • 33
  • 51
3
votes
1 answer

Detect that a struct contains a flexible array member

Say that I have a struct like this struct foo { int n; int values[]; }; Is it possible to detect the flexible array member using SFINAE? At least, I can construct a class template that cannot be instantiated with such struct: template
user877329
  • 6,717
  • 8
  • 46
  • 88
3
votes
1 answer

What is the rationale for "structure with flexible array member shall not be a member of a structure"?

C11, 6.7.2.1 Structure and union specifiers, Constraints, 3 (emphasis added): A structure or union shall not contain a member with incomplete or function type (hence, a structure shall not contain an instance of itself, but may contain a pointer to…
pmor
  • 5,392
  • 4
  • 17
  • 36
3
votes
4 answers

Flexible array member without having to be the last one

I am trying to figure out whether there is a workaround in C to have a flexible array member in a struct(s), that is not the last one. For example, this yields compilation error: typedef struct __attribute__((__packed__)) { uint8_t …
Łukasz Przeniosło
  • 2,725
  • 5
  • 38
  • 74
3
votes
4 answers

Why is this initialization of a structure with a flexible array member invalid but valid with an fixed size array member?

The C standard states (emphasize mine): 21 EXAMPLE 2 After the declaration: struct s { int n; double d[]; }; the structure struct s has a flexible array member d. [...] 22 Following the above declaration: struct s t1 = { 0 }; //…
1 2
3
8 9