C struct hack at work

后端 未结 4 1437
野趣味
野趣味 2020-11-28 06:39

Is this how one can use the the \"extra\" memory allocated while using the C struct hack?

Questions:

I have a C struct hack implementation below. My questio

4条回答
  •  一生所求
    2020-11-28 06:52

    It is 'correct', but you'd need a good reason to do that over a more reasonable solution. More commonly perhaps you'd use this technique to "overlay" some existing array to impose some sort of header structure on to it.

    Note that GCC by extension allows a zero length array member for exactly this purpose, while ISO C99 "legitimises" the practice by allowing a member with empty brackets (only as the last member).

    Note that there are some semantic issues - sizeof the struct will not of course account for the "flexible" size of the final member, and passing the struct "by value" will only pass the header and first element (or no element using the GCC extension or C99 flexible array member). Similarly direct struct assignment will not copy all the data.

提交回复
热议问题