5 comments

  • Arch-TK28 minutes ago
    That weird feeling when you realise that the people you hang out with form such a weird niche that something considered common knowledge among you is being described as &quot;buried deep within the C standard&quot;.<p>What&#x27;s noteworthy is that the compiler isn&#x27;t required to generate a warning if the array is too small. That&#x27;s just GCC being generous with its help. The official stance is that it&#x27;s simply undefined behaviour to pass a pointer to an object which is too small (yes, only to pass, even if you don&#x27;t access it).
  • nikeee30 minutes ago
    GCC also has an extension to support references to other parameters of the function:<p><pre><code> #include &lt;stddef.h&gt; void foo(size_t n, int b[static n]); </code></pre> <a href="https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;c4o7hGaG1" rel="nofollow">https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;c4o7hGaG1</a><p>It is not limited to compile-time constants. Doesn&#x27;t work in clang, sadly.
    • fuhsnn10 minutes ago
      Clang is working on a different version with annotations <a href="https:&#x2F;&#x2F;www.open-std.org&#x2F;jtc1&#x2F;sc22&#x2F;wg14&#x2F;www&#x2F;docs&#x2F;n3656.pdf" rel="nofollow">https:&#x2F;&#x2F;www.open-std.org&#x2F;jtc1&#x2F;sc22&#x2F;wg14&#x2F;www&#x2F;docs&#x2F;n3656.pdf</a>
  • Veserv47 minutes ago
    Pointer to array is not only type-safe, it is also objectively correct and should have always been the syntax used when passing in the address of a known, fixed size array. This is all a artifact of C automatically decaying arrays to pointers in argument lists when a array argument should have always meant passing a array by value; then this syntax would have been the only way to pass in the address of a array and we would not have these warts. Automatic decaying is truly one of the worst actual design mistakes of the language (i.e. a error even when it was designed, not the failure to adopt new innovations).
    • jacquesm43 minutes ago
      Fully agreed, and something that is hard to fix. This guy is trying really hard and with some success:<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=45735877">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=45735877</a>
      • wild_pointer15 minutes ago
        This guy is doing something else completely. In his words:<p>&gt; In my testing, it&#x27;s between 1.2x and 4x slower than Yolo-C. It uses between 2x and 3x more memory. Others have observed higher overheads in certain tests (I&#x27;ve heard of some things being 8x slower). How much this matters depends on your perspective. Imagine running your desktop environment on a 4x slower computer with 3x less memory. You&#x27;ve probably done exactly this and you probably survived the experience. So the catch is: Fil-C is for folks who want the security benefits badly enough.<p>(from <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=46090332">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=46090332</a>)<p>We&#x27;re talking about a lack of fat pointers here, and switching to GC and having a 4x slower computer experience is not required for that.
  • o11c53 minutes ago
    Better option: just wrap it in a unique struct.<p>There are perhaps only 3 numbers: 0, 1, and lots. A fair argument might be made that 2 also exists, but for anything higher, you need to think about your abstraction.
  • aaaashley1 hour ago
    Funny thing about that n[static M] array checking syntax–it was even considered bad in 1999, when it was included:<p>&quot;There was a unanimous vote that the feature is ugly, and a good consensus that its incorporation into the standard at the 11th hour was an unfortunate decision.&quot; - Raymond Mak (Canada C Working Group), <a href="https:&#x2F;&#x2F;www.open-std.org&#x2F;jtc1&#x2F;sc22&#x2F;wg14&#x2F;www&#x2F;docs&#x2F;dr_205.htm" rel="nofollow">https:&#x2F;&#x2F;www.open-std.org&#x2F;jtc1&#x2F;sc22&#x2F;wg14&#x2F;www&#x2F;docs&#x2F;dr_205.htm</a>
    • jacquesm45 minutes ago
      It wasn&#x27;t considered bad, it was considered ugly and in the context given that is a major difference. The proposed alternative in that post to me is even more ugly so I would have agreed with the option that received the most support, to leave it as it was.
      • moefh1 minute ago
        It was always considered bad not (just) because it&#x27;s ugly, but because it hides potential problems and adds no safety at all: a `[static N]` parameter tells the compiler that the parameter will never be NULL, but the function can still be called with a NULL pointer anyway.<p>That&#x27;s is the current state of both gcc and clang: they will both happily, without warnings, pass a NULL pointer to a function with a `[static N]` parameter, and then REMOVE ANY NULL CHECK from the function, because the argument can&#x27;t possibly be NULL according to the function signature, so the check is obviously redundant.<p>See the example in [1]: note that in the assembly of `f1` the NULL check is removed, while it&#x27;s present in the &quot;unsafe&quot; `f2`, making it actually safer.<p>Also note that gcc will at least tell you that the check in `f1()` is &quot;useless&quot; (yet no warning about `g()` calling it with a pointer that could be NULL), while clang sees nothing wrong at all.<p>[1] <a href="https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;ba6rxc8W5" rel="nofollow">https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;ba6rxc8W5</a>