Re:Tell HN: C Experts Panel – Ask us anything about C
by enriquto on Tuesday April 14, 2020 @04:10PM
(#22870854)
That's a really bizarre layout for your struct. Why don't you put the length first?
[ Parent
| on HN ]
Re:Tell HN: C Experts Panel – Ask us anything about C
by twic on Tuesday April 14, 2020 @04:21PM
(#22870982)
Why would it matter? The bytes aren't inline, this is just a struct with two word-sized fields.
A possible tiny advantage for this layout is that a pointer to this struct can be used as a pointer to a pointer-to-bytes, without having to adjust it. Although i'm not sure that's not undefined behaviour.
Re:Tell HN: C Experts Panel – Ask us anything about C
by epr on Wednesday April 15, 2020 @09:30AM
(#22877447)
I don't think that's undefined behavior. That's how C's limited form of polymorphism is utilized. For example, many data structures behind dynamic languages are implemented in this way. A concrete example would be Python's PyObject which share PyObject_HEAD.
https://github.com/python/cpython/blob/master/Include/object...
Re:Tell HN: C Experts Panel – Ask us anything about C
by ATsch on Tuesday April 14, 2020 @04:17PM
(#22870919)
I'm not sure if it matters. It might be better for some technical reason, such as speeding up double dereferences, because you don't need to add anything to get to the pointer. But to be honest I just copied it out of existing code.