TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Learning that you can use unions in C for grouping things into namespaces

167 点作者 deafcalculus将近 4 年前

11 条评论

10000truths将近 4 年前
Anonymous nested structs are also quite useful for creating struct fields with explicit offsets:<p><pre><code> #include &lt;stdio.h&gt; #include &lt;stdint.h&gt; #define YDUMMY(suffix, size) char dummy##suffix[size] #define XDUMMY(suffix, size) YDUMMY(suffix, size) #define PAD(size) XDUMMY(__COUNTER__, size) struct ExplicitLayoutStruct { union { struct __attribute__((packed)) { PAD(3); uint32_t foo; }; struct __attribute__((packed)) { PAD(5); uint16_t bar; }; struct __attribute__((packed)) { PAD(13); uint64_t baz; }; }; }; int main(void) { &#x2F;&#x2F; offset foo = 3 &#x2F;&#x2F; offset bar = 5 &#x2F;&#x2F; offset baz = 13 printf(&quot;offset foo = %d\n&quot;, offsetof(struct ExplicitLayoutStruct, foo)); printf(&quot;offset bar = %d\n&quot;, offsetof(struct ExplicitLayoutStruct, bar)); printf(&quot;offset baz = %d\n&quot;, offsetof(struct ExplicitLayoutStruct, baz)); return 0; }</code></pre>
评论 #28029184 未加载
评论 #28028032 未加载
评论 #28032024 未加载
评论 #28028231 未加载
评论 #28029168 未加载
flohofwoe将近 4 年前
I&#x27;m using anonymous nested structs extensively for grouping related items, but I consider the extra field name a feature, not something that should be hidden:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;floooh&#x2F;sokol-samples&#x2F;blob&#x2F;bfb30ea00b5948f2029c7dd228cf30de08b8036c&#x2F;sapp&#x2F;shdfeatures-sapp.c#L136-L165" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;floooh&#x2F;sokol-samples&#x2F;blob&#x2F;bfb30ea00b5948f...</a><p>(also note the &#x27;inplace initialization&#x27; which follows the state struct definition using C99&#x27;s designated initialization)
kevin_thibedeau将近 4 年前
The result is uglier and less maintainable than a pair of macros. Or just stop trying to hide syntax. This is ultimately on the same level as typedefing pointers.
remram将近 4 年前
The first example seems wrong, instead of `struct sub { ... };` what is meant is `struct { ... } sub;`
评论 #28030713 未加载
sesuximo将近 4 年前
Doesn’t matter for C, but in C++ this could make your contexpr functions UB since you can only use one member of a union in constexpr contexts (the “active” member).
评论 #28027362 未加载
评论 #28027476 未加载
评论 #28027206 未加载
评论 #28029207 未加载
bruce343434将近 4 年前
Imo this is not “perverse”. In my vector library I alias a vec3 as float x,y,z and float[3] using this technique.
评论 #28029196 未加载
PaulHoule将近 4 年前
The C programming language, brought to you by Cthulhu.<p>You don&#x27;t need eval(), you&#x27;ve got strcpy()!
rightbyte将近 4 年前
I don&#x27;t regard this as a &quot;perverse&quot; hack. If I ever do embedded memory mapped stuff in C11 this is way too tempting.
评论 #28029221 未加载
Subsentient将近 4 年前
Bleurgh. I have a deep soft spot for C, and I&#x27;m known to get twisted pleasure from using obscure language features in new ways to annoy people, but this is a level of abuse that even I can&#x27;t get behind. If you need namespacing, use C++. As much as I love C, it&#x27;s terrible for large projects.
评论 #28028854 未加载
评论 #28030070 未加载
评论 #28028398 未加载
midjji将近 4 年前
This is probably a terrible idea, remember that if you have written one member of a union, all other members remain public, yet accessing any of them in any way is undefined behaviour. This is made way worse by most compilers mostly choosing to let you do what you think it will. They just dont guarantee they always will or in all cases.
评论 #28029590 未加载
adamnemecek将近 4 年前
Don&#x27;t actually do this.
评论 #28027729 未加载