Are flexible array elements needed?

A structure with a flexible member of an array is apparently not intended to be declared, but is used in combination with a pointer to this structure. When declaring an element of a flexible array, there must be at least one other member, and the flexible member of the array must be the last member of this structure.

Say I have one that looks like this:

struct example{ int n; int flm[]; } 

Then, to use it, I will have to declare a pointer and use malloc to reserve memory for the contents of the structure.

 struct example *ptr = malloc(sizeof(struct example) + 5*sizeof(int)); 

That is, if I want my flm [] array to contain five integers. Then I can just use my structure for example:

 ptr->flm[0] = 1; 

My question is: shouldn't I just use a pointer instead? This is not only compatible with pre-C99, but I could use it with or without a pointer to this structure. Given that I should already use malloc with flm, shouldn't I just do this?

Consider this new definition of an example struct;

 struct example{ int n; int *notflm; } struct example test = {4, malloc(sizeof(int) * 5)}; 

I would even use a replacement in the same way as a flexible array element:

Will this work? (Subject to the above definition of an example with notflm)

 struct example test; test.n = 4; notflm = malloc(sizeof(int) * 5); 
+10
c struct flexible-array-member
Aug 01 '13 at 18:25
source share
2 answers

Pointers are not arrays. The main reasons for choosing what to use are the same as always with arrays against pointers. In the particular case of flexible array members, here are a few reasons why you might prefer them by pointer:

  • Reduced storage requirements. A pointer will increase your structure with (usually) 4 or 8 bytes, and you will spend a lot more overhead if you allocate dedicated storage separately and not on a single malloc call.

  • Improving access efficiency. The flexible array element is located at a constant offset from the base structure. The pointer requires separate dereferencing. This affects both the number of instructions required to access it and the pressure in the register.

  • Atomicity of success / failure of distribution. If you select a structure and allocate storage so that it points to two separate steps, your code for cleaning in case of failures will be much uglier, since you have a case where one failed and the other failed. This can be avoided with some pointer arithmetic in order to cut both from the same malloc request, but it is easy to get a logical error and cause UB due to alignment problems.

  • Avoid the need for deep copying. If you use a flexible array instead of a pointer, you can simply memcpy (not assign, because the assignment cannot know the length of the flexible array) to copy the structure rather than copy the pointed data, and fix the pointer in a new copy.

  • Avoid the need for deep freedom. It is very convenient and clean to be able to just use one object free , and not have pointy data free . This can also be achieved with the “cut-out single malloc approach” mentioned above, of course, but flexible arrays make it easier and less error prone.

  • Of course, there are many more reasons ...

+23
Aug 01 '13 at 18:35
source share

These concepts are definitely not needed, as you yourself indicated.

The differences between the two that you demonstrated are where your data resides in memory.

In the first example with a flexible array, your metadata and the array itself are in the same memory block and can be moved as a single block (pointer) if you need to.

In the second example, your metadata is on the stack, and your array is somewhere else on the heap. To move / copy it, you will need to move two blocks of memory and update the pointer in your metadata structure.

Usually flexible size arrays are used when you need to place an array and metadata spatially together in memory.

An example where this is definitely useful, for example, when placing an array with metadata in a file, you have only one contiguous block of memory, and each time you load it, it will (most likely) be placed elsewhere in your virtual machine.

0
Aug 01 '13 at 18:36
source share



All Articles