Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is, in effect, <strong>a way to check whether the expression e can be evaluated to be 0, and if not, to fail the build</strong>.</p> <p>The macro is somewhat misnamed; it should be something more like <code>BUILD_BUG_OR_ZERO</code>, rather than <code>...ON_ZERO</code>. (There have been <strong><a href="http://lkml.indiana.edu/hypermail/linux/kernel/0703.1/1546.html" rel="noreferrer">occasional discussions about whether this is a confusing name</a></strong>.)</p> <p>You should read the expression like this:</p> <pre><code>sizeof(struct { int: -!!(e); })) </code></pre> <ol> <li><p><code>(e)</code>: Compute expression <code>e</code>.</p></li> <li><p><code>!!(e)</code>: Logically negate twice: <code>0</code> if <code>e == 0</code>; otherwise <code>1</code>.</p></li> <li><p><code>-!!(e)</code>: Numerically negate the expression from step 2: <code>0</code> if it was <code>0</code>; otherwise <code>-1</code>.</p></li> <li><p><code>struct{int: -!!(0);} --&gt; struct{int: 0;}</code>: If it was zero, then we declare a struct with an anonymous integer bitfield that has width zero. Everything is fine and we proceed as normal.</p></li> <li><p><code>struct{int: -!!(1);} --&gt; struct{int: -1;}</code>: On the other hand, if it <em>isn't</em> zero, then it will be some negative number. Declaring any bitfield with <em>negative</em> width is a compilation error.</p></li> </ol> <p>So we'll either wind up with a bitfield that has width 0 in a struct, which is fine, or a bitfield with negative width, which is a compilation error. Then we take <code>sizeof</code> that field, so we get a <code>size_t</code> with the appropriate width (which will be zero in the case where <code>e</code> is zero).</p> <hr/> <p>Some people have asked: <strong>Why not just use an <code>assert</code>?</strong></p> <p><a href="https://stackoverflow.com/a/9230305/75170">keithmo's answer</a> here has a good response:</p> <blockquote> <p>These macros implement a compile-time test, while assert() is a run-time test.</p> </blockquote> <p>Exactly right. You don't want to detect problems in your <em>kernel</em> at runtime that could have been caught earlier! It's a critical piece of the operating system. To whatever extent problems can be detected at compile time, so much the better.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload