Is this a flexible element of an array structure in C?

Possible duplicate:
Flexible array members in C - bad?

I read the following code:

struct hello { int number; int data[1]; }; 

I know that members of a flexible array allow you to declare the last element of an array of unspecified size as follows:

 struct hello { int number; int data[]; }; 

In this struct last element does not determine the size, so what is the difference between the two? and what does the first declaration mean?

0
source share
4 answers

This array:

 struct hello { int number; int data[]; }; 

it makes sense only as the last element of the structure and only when distributing the structure from the heap and, as a rule, so that the header fields contain the actual size of the array:

 struct hello *helloptr = malloc(sizeof (struct hello) + count*(sizeof int)); helloptr->number = count; 

The use is to distribute the header and data buffer conveniently with a single structure.

Addition: the difference in your version, which allocates an array of size 1, sizeof struct hello will be larger, since the array is allocated with one element instead of undefined = zero. This either takes up one piece of memory or makes size calculations more complex and a little ugly conceptually (your code is 1 array in size, when you actually mean "undefined", maybe even 0).

In OOP C ++, it’s better to just wrap a dynamically allocated buffer inside the class, this is really a β€œhack”.

+1
source

What is the difference between data[1] and data[0] ?

There is no difference, both can be used to implement a flexible size structure.

Raymond Chen answers him aptly :

Well, you can tell, then why not use an array of zero length instead of an array with a length of 1 row?

Because time travel has not yet been completed.

Zero-length matrices did not become standard C standards until 1999. Since Windows was long before that, he could not take advantage of this functionality in C.

Please note that technically the second code example is not valid. C ++ code. [Ref 1] But a lot of existing code uses this function, and this code will almost always work.


[Link 1] Link:

C ++ 11 Standard: 8.3.4 Arrays

In the declaration T D , where D has the form

 D1 [ constant-expressionopt] attribute-specifier-seqopt 

....... If the constant expression (5.19) is present, it must be an integral constant expression, and its value must be greater than zero .

0
source

It is not a flexible element of an array, but is used in some code that should work with older compilers or with C ++ compilers.

Using it as an element of a flexible array can cause undefined behavior. In real code, this should work as expected, as this trick is used in too much code for the compiler to do any unexpected things.

0
source

Both are not completely equivalent in C, even if you only access the first element. A flexible array can have different alignment compared to a fixed-size array. Therefore, even sizeof can give you different results for both, and you should never mix these two options inside the same code.

0
source

Source: https://habr.com/ru/post/959121/


All Articles