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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Getting silly with C, part (void*)2

175 点作者 justmarc4 个月前

17 条评论

sylware4 个月前
C syntax is already way too rich and complex.<p>We need a C- ore µC:<p>No implicit cast except for literals and void* (explicit compile time&#x2F;runtime casts), one loop statement (loop{}), no switch&#x2F;enum&#x2F;generic&#x2F;_thread&#x2F;typeof&#x2F;etc, no integer promotion, only sized primitive types (u64 s32 f32 etc...), no anonymous code block, real compiler hard&#x2F;compile time constant declaration, many operators have to go (--,++, a?b:c, etc)... and everything I am forgetting right now (the dangerous struct pack attribute...). But we need inline keywords for memory barriers, atomics for modern hardware architecture programming.
评论 #42659765 未加载
评论 #42658687 未加载
评论 #42659028 未加载
评论 #42658617 未加载
评论 #42658590 未加载
评论 #42658897 未加载
mhandley4 个月前
I expect many people know this one, but it&#x27;s a useful teaching aid when understanding the relationship between arrays and pointers<p><pre><code> int array[10]; *(array+1) = 56; array[2] = 4; 3[array] = 27; </code></pre> The first two are obvious, but the third is also legal. It works because array indexing is just sugar for pointer arithmetic, so array[2]=4 is identical in meaning to *(array+2)=4. Therefore 3[array]=27 is identical to *(3+array)=27 and so is legal. But just because you can doesn&#x27;t mean you should.
评论 #42662034 未加载
评论 #42662212 未加载
matheusmoreira4 个月前
Note that this is GNU C, not standard C. GNU has extended the normal C language with features such as forward parameter declarations and numeric ranges in switch cases. Lots of people don&#x27;t know about these things.
评论 #42658824 未加载
dfawcus4 个月前
I&#x27;d have to argue the function typedefs are not useless, I&#x27;ve come across two uses.<p>The obvious one is rather than a function pointer typedef, such the subsequent use in a struct is obviously a pointer. Which helps when others are initially reading unfamiliar structures.<p><pre><code> typedef int handler_ty(int a); struct foo { handler_ty *handler; &#x2F;* ... *&#x2F; } struct foo table[] = { { &#x2F;* init fields *&#x2F;, &#x2F;* init fields *&#x2F;, }; </code></pre> The other case can be somewhat related, namely as an assertion &#x2F; check when writing such handler functions, and more importantly updating them.<p><pre><code> handler_ty some_handler; int some_handler(int a) { &#x2F;* ... *&#x2F; } </code></pre> When updating code, it allowed for easier to decode compiler errors if the expected type of handler_ty was changed, and some specific handler was incorrectly updated, or not updated at all.<p>Basically the error would generally directly call out the inconsistency with the prior line, rather than with the distanct use in the initialisation of &#x27;table&#x27;.<p>As I recall this mechanism has been around since at least C89, I don&#x27;t recall using it in K&amp;R.
WalterBright4 个月前
I&#x27;m going to speculate a bit on why these silly things are in C.<p>C was developed on a PDP-11 that had 64Kb of memory. That&#x27;s not much of any at all. Therefore, the compiler must be extremely tightly coded.<p>The fundamental rules of the C language are pretty simple. But articles like these expose consequences of such simple rules. Fixing them requires adding more code. Adding more code means less room for the code being compiled.<p>Therefore, if the intended use of the language works, the pragmatic approach would be to simply not worry about the quirky consequences.<p>A more interesting question would be &quot;why do these characteristics persist in modern C compilers?&quot;<p>The stock answer is &quot;backwards compatibility&quot;, &quot;Obfuscated C Code contests&quot; and &quot;gotcha job interview questions&quot;. My argument would be that there is no reason for the persistence of such &quot;junk DNA&quot; and it should be deprecated and removed.<p>I&#x27;ve done my part. D doesn&#x27;t support that stuff, even though the basic use of the language is easily confused with C.<p>For example:<p><pre><code> #include &lt;stdio.h&gt; void main() { int i; for (i = 0; i &lt; 10; ++i); printf(&quot;%d\n&quot;, i); } </code></pre> I&#x27;ve died on that hill. I know others who lost an <i>entire day</i> staring at it wondering what&#x27;s wrong with it. I saw it on X recently as &quot;99% of C programmers will not be able to find the bug.&quot;<p>The equivalent D code:<p><pre><code> import core.stdc.stdio; void main() { int i; for (i = 0; i &lt; 10; ++i); printf(&quot;%d\n&quot;, i); } </code></pre> gets you:<p><pre><code> test.d(5): Error: use `{ }` for an empty statement, not `;` </code></pre> C&#x27;mon, Standard C! Fix that!
评论 #42662666 未加载
评论 #42662420 未加载
评论 #42666667 未加载
评论 #42662974 未加载
mystified50164 个月前
Forward parameter declaration is an insane feature. It makes perfect sense in the context of C&#x27;s other forward declarations but just bonkers.<p>I can&#x27;t wait to slip this into some production code to confuse the hell out of some intern in a few years
svilen_dobrev4 个月前
hehe. similar to<p>How to Get Fired Using Switch Statements &amp; Statement Expressions:<p><a href="https:&#x2F;&#x2F;blog.robertelder.org&#x2F;switch-statements-statement-expressions&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.robertelder.org&#x2F;switch-statements-statement-exp...</a>
kazinator4 个月前
Without information about how identifiers are declared, you do not know how to parse this:<p><pre><code> (A)(B); </code></pre> It could be a cast of B to type A, or function A being called with argument B.<p>Or this (like the puts(puts) in the article):<p><pre><code> A(B): </code></pre> Could be a declaration of B as an identifier of type A, or a call to a function A with argument B.<p>Back in 1999 I made a small C module called &quot;sfx&quot; (side effects) which parses and identifies C expressions that could plausibly contain side effects. This is one of the bits provided in a small collection called Kazlib.<p>This can be used to make macros safer; it lets you write a #define macro that inserts an argument multiple times into the expansion. Such a macro could be unsafe if the argument has side effects. With this module, you can write the macro in such a way that it will catch the situation (albeit at run time!). It&#x27;s like a valgrind for side effects in macros, so to speak.<p><a href="https:&#x2F;&#x2F;git.savannah.gnu.org&#x2F;cgit&#x2F;kazlib.git&#x2F;tree&#x2F;sfx.c" rel="nofollow">https:&#x2F;&#x2F;git.savannah.gnu.org&#x2F;cgit&#x2F;kazlib.git&#x2F;tree&#x2F;sfx.c</a><p>In the sfx.c module, there is a rudimentary C expression parser which has to work in the absence of declaration info. In other words it has to make sense of an input like (A)(B).<p>I made it so that when the parser encounters an ambiguity, it will try parsing it both ways, using backtracking via exception handling (provided by except.c). When it hits a syntax error, it can backtrack to an earlier point and parse alternatively.<p>Consider (A)(A+B). When we are looking at the left part (A), that could plausibly be a cast or declaration. In recursive descent mode, we are going left to right and looking at left derivations. If we parse it as a declaration, we will hit a syntax error on the +, because there is no such operator in the declarator grammar. So we backtrack and parse it as a cast expression, and then we are good.<p>Hard to believe that was 26 years ago now. I think I was just on the verge of getting into Lisp.<p>I see the sfx.c code assumes it would never deal with negative character values, so it cheerfully uses the &lt;ctype.h&gt; functions without a cast to unsigned char. It&#x27;s a reasonable assumption there since the inputs under the intended use case would be expressions in the user&#x27;s program, stringified by the preprocessor. Funny bytes would only occur in a multi-byte string literal (e.g. UTF-8). When I review code today, this kind of potential issue immediately stands out.<p>The same exception module is (still?) used in the Ethereal&#x2F;Wireshark packet capture and analysis tool. It&#x27;s used to abort &quot;dissecting&quot; packets that are corrupt or truncated.
jwilk4 个月前
First part discussed on HN:<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=40835274">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=40835274</a> (113 comments)
zzo38computer4 个月前
I had read the GCC documentation and I did not know about the forward parameter declaration. I did know about the other stuff that is mentioned there (and in the first part).<p>Declarations in for loops is something that I had only ever used in macros (I had not found it useful in other circumstances), such as:<p><pre><code> #define lpt_document() for(int lpt_document_=lpt_begin();lpt_document_;lpt_document_=(lpt_end(),0)) #define win_form(xxx) for(win_memo win_mem=win_begin_();;win_step_(&amp;win_mem,xxx)) </code></pre> (The compiler will optimize out the loop and the declared variable in the use of the lpt_document macro; I had tested this.)
teddyh4 个月前
The <i>comp.lang.c Frequently Asked Questions</i> &lt;<a href="https:&#x2F;&#x2F;c-faq.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;c-faq.com&#x2F;</a>&gt; should be required reading for every serious C programmer.
评论 #42663984 未加载
GrantMoyer4 个月前
I should keep this link handy for when people claim C is a simple language. Even without the GNU extensions, the examples here are pretty wretched.
评论 #42663293 未加载
评论 #42664021 未加载
betimsl4 个月前
Where can I find more about this BASIC compatibility mode? Thnx
评论 #42662140 未加载
GranPC4 个月前
Can anyone explain how the last snippet works?
评论 #42658789 未加载
评论 #42658628 未加载
utopcell4 个月前
C[++] never ceases to amaze me. Just yesterday I saw this, totally valid, code snippet:<p>void g(); void f() { return g(); }
评论 #42661271 未加载
评论 #42660559 未加载
AKluge4 个月前
An old classic: Remember, C was never intended to be taken seriously. <a href="https:&#x2F;&#x2F;www.cs.cmu.edu&#x2F;~jbruce&#x2F;humor&#x2F;unix_hoax.html" rel="nofollow">https:&#x2F;&#x2F;www.cs.cmu.edu&#x2F;~jbruce&#x2F;humor&#x2F;unix_hoax.html</a>
评论 #42663495 未加载
评论 #42662154 未加载
psychoslave4 个月前
Usually I&#x27;m more in the camp of &quot;let&#x27;s preserve everything we can as cultural heritage, yes even those awful Nazi propaganda material&quot; and I&#x27;m confident that some distant archeologist (or current close neighbor) will be glad we did.<p>But as time pass, I&#x27;m more and more convinced that wiping-out every peace of C that was ever produced would be one of the greatest possible gesture for the future of humanity.<p>I also have a theory that universe exits so we can have some opportunities to taste chocolate. Surely in that perspective, even C can be an unfortunate but acceptable byproduct.
评论 #42661171 未加载
评论 #42661008 未加载
评论 #42661188 未加载
评论 #42661077 未加载