Struct nesting, flexible arrays

Asked by Christof Mroz

Is there a reason why the fields of struct hip_tlv_common are repeated for all parameter struct, rather than nesting struct hip_tlv_common as the first member? This would get rid of casts and code duplication.
See http://c-faq.com/struct/oop.jxh.html (edit: note that the cast given in the example can actually be avoided)

Also, why is pointer arithmetic preferred over using flexible arrays? Those would be more readable, less error-prone (well, than pointer arithmetic at least) and (in principle) be forward-compatible.
http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Zero-Length.html (the part about [])

Question information

Language:
English Edit question
Status:
Solved
For:
HIPL Edit question
Assignee:
No assignee Edit question
Solved by:
Miika Komu
Solved:
Last query:
Last reply:
Revision history for this message
Stefan Götz (stefan.goetz-deactivatedaccount) said :
#1

Well, I'm not an author of the original code, but my guess is that there is actually no strong reason for this. On the other hand, I don't see the resources to actually change the status quo.

Revision history for this message
Best Miika Komu (miika-iki) said :
#2

Yes, struct hip_tlv_common could be reused better as long as it's a packed structure. Just a lot of places to change for this.

Flexible arrays is gcc specific, but I think we're not going for other compilers any time time soon. So why not.

Revision history for this message
Christof Mroz (christof-mroz) said :
#3

Thanks Miika Komu, that solved my question.

Revision history for this message
Christof Mroz (christof-mroz) said :
#4

On Wed, 30 Mar 2011 20:41:21 +0200, Miika Komu
<email address hidden> wrote:

> Miika Komu proposed the following answer:
> Yes, struct hip_tlv_common could be reused better as long as it's a
> packed structure.

Packedness ist not an issue with nested structs btw.

> Just a lot of places to change for this.

True. I didn't want to propose a change at all (the current code is not
broken), just curious.

> Flexible arrays is gcc specific, but I think we're not going for other
> compilers any time time soon. So why not.

They're in C99, IIRC.

Revision history for this message
Diego Biurrun (diego-biurrun) said :
#5

Please let's do this. I have had a look at the structs in lib/core/protodefs.h the other day and the amount of duplication is just staggering. Nesting structures would furthermore eliminate a very large part of the remaining casts in HIPL.