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
0
votes
2 answers

What is the most portable way to store both integers and generic pointers in C?

I am developing a system (virtual machine? not sure how to call this) in which every data structure is an array (or struct) with three integer fields at the beginning (which must be at the beginning right after another without padding bytes) and n…
user3810155
0
votes
1 answer

Issue with devm_kzalloc

I am trying to understanding devm_kzalloc() function implementation. It is allocating more than the requested memory(sizeof(struct devres) + size) to manage resources. struct devres is defined as follows, the second member is an incomplete…
user3693586
  • 1,227
  • 5
  • 18
  • 40
0
votes
2 answers

Allocate memory for flexible array in structure

I am trying to allocate memory for a structure using a flexarray. I received it this way and I have to implement it like this. The structure looks like: struct _XPM { unsigned int width; unsigned int height; unsigned char cpp; …
Sebastian Luke
  • 385
  • 5
  • 18
0
votes
2 answers

Run-time sized array in a c++ struct

In C (with gcc), I used to have some data structures that were an array with some extra information: struct song { uint tempo; uint key; note play[0]; // or play[] depending on compiler flavour }; Iirc, that's dubbed a "flexible array"…
PypeBros
  • 2,607
  • 24
  • 37
-1
votes
4 answers

Function to generate struct instances in c

How do I write a function that generates an instance of a previously defined struct every time it's called? I'm sure since it's an easy problem no context is needed but here is what I have now. #include #include #include…
-1
votes
1 answer

How to handle flexible Arrays in C++

Context So I've been playing around with arrays in C/ C++ trying to create arrays that could dynamically add and remove their elements. Of course, I figured that the flexible array member feature in C would be the appropriate way to go. So I begun…
Lapys
  • 936
  • 2
  • 11
  • 27
-1
votes
3 answers

Finding the size of the structure dynamically

Is there any way to find the structure size dynamically in C?? Sizeof is compile time operator.. so what's the other option. If we can have dynamically allocated array(flexible arrays) in a structure, then finding the structure size dynamically…
Siri
  • 29
  • 1
  • 5
-1
votes
2 answers

Using the struct itself in the definition of struct in C++

I want to implement a simple tree struct in C++, like: struct node{ .... node* parent; node[]* children; .... }; but the compiler reported an error (both CLang++ and G++) error: expected unqualified-id before '[' token node[]*…
Z.Shang
  • 16
  • 4
-2
votes
1 answer

Error: Invalid use of flexible array member

I don't know why this code give this error. What should I do? The error is as follows: Invalid use of flexible array member on this line: new_buffer->array_msg =array; here if are larger section of code: typedef struct buffer { int size; …
user2993592
  • 31
  • 2
  • 6
-3
votes
2 answers

Memory corruption of dynamic array in struct?

So I have these two structs. Only need to worry about these three variables; name_size, name and xattrs[0]. typedef struct dxattr { unsigned int name_size; /* length of name string in bytes */ unsigned int value_offset; /* offset…
Keith
  • 35
  • 8
1 2 3
8
9