Home > Back-end >  Variable-length structure on how to use
Variable-length structure on how to use

Time:09-17

Excuse me this kind of multilayer nested variable-length structure, how to apply for A memory will be more reasonable, I met A problem is that A structure of a_val_1 [0] and a_val [1] is only the address difference between sizeof (B), which is two BYTE long, leading to a_val_1 [0]. B_val_2 [0] c_val_1 and c_val_2 two values are a_val_1 b_val_1 and b_len covered [1], [Linux development environment)
Struct C
{
BYTE c_val_1;
BYTE c_val_2;
}
Struct B
{
BYTE b_val_1;
BYTE b_len;
Struct b_val_2 C [0];
}
Struct A
{
BYTE a_len;
Struct B a_val_1 [0];
}


And have a longer structure exists in the two variables, how to deal with the
Struct D
{
BYTE d_len.
BYTE d_val_1 [0].
BYTE d_val_2 [0].
}

CodePudding user response:

You use this nested zero-length arrays, belong to "abuse of zero-length array" behavior; Because 0 length of the array is actually not allocate memory, just have the effect of A placeholder, such as struct a_val_1 understandable into strcut B * p_a_val_1;

The compiler will not assign you a p_a_val_1 space, need application by means of dynamically allocated memory of malloc allocation, released immediately after use.

Here is to expose its an important faults: in structure, an array of 0 array must be in the final statement, has certain limitation, make use the

Back to the host's problem: use nested overlay, like the original poster is short of the desired result,
Remember: 0 length of the array is the use of flexible array point is behind the continuous memory space,

CodePudding user response:

Multiple nested structure, no problem,
  • Related