<story><title>Some dark corners of C</title><url>https://docs.google.com/presentation/d/1h49gY3TSiayLMXYmRMaAEMl05FaJ-Z6jDOWOz3EsqqQ/edit?usp=sharing</url></story><parent_chain><item><author>gjulianm</author><text>&lt;p&gt;&lt;pre&gt;&lt;code&gt;    #define struct union
    #define else
&lt;/code&gt;&lt;/pre&gt;
That&apos;s evil. I have to do it in someone&apos;s code some day just to have some fun.&lt;p&gt;But, apart from that, it&apos;s a really nice compilation. I didn&apos;t know about the compile time checks of array sizes, but I have a doubt. What if I pass to a method declared&lt;p&gt;&lt;pre&gt;&lt;code&gt;    int foo(int x[static 10])
&lt;/code&gt;&lt;/pre&gt;
this pointer&lt;p&gt;&lt;pre&gt;&lt;code&gt;    int* x = (int*) calloc(20, sizeof(int));
&lt;/code&gt;&lt;/pre&gt;
Does the compiler skip the check? Does it give me a warning?&lt;p&gt;EDIT: Funnily enough, in Mac it doesn&apos;t give any warning, neither for pointers nor for undersized arrays (ie, foo(w[5]) doesn&apos;t give a warning). And I&apos;ve compiled with -std=c99 -pedantic -Wall.</text></item></parent_chain><comment><author>tobiasu</author><text>Last time this came up on HN, it was thought to be a clang feature.&lt;p&gt;Edit: While we&apos;re talking about dark corners, please stop casting functions that return void * . If your code lacks the declaration of the function, the compiler will assume pre ANSI-C semantics and generate code returning an &lt;i&gt;int&lt;/i&gt;.&lt;p&gt;On machines where pointers do not fit ints (basically all 64bit machines), you just silently (due to the cast there is no warning) truncated a pointer. Worse, it may work depending on the malloc implementation and how much memory you allocate.&lt;p&gt;We have to fix these kinds of bugs on OpenBSD a lot, please help by typing less and let the compiler warn you about silly mistakes :-)&lt;p&gt;And yes, C++ fucked this up for C. I&apos;ll leave it to Linus to say something nice about that..</text></comment>
<story><title>Some dark corners of C</title><url>https://docs.google.com/presentation/d/1h49gY3TSiayLMXYmRMaAEMl05FaJ-Z6jDOWOz3EsqqQ/edit?usp=sharing</url></story><parent_chain><item><author>gjulianm</author><text>&lt;p&gt;&lt;pre&gt;&lt;code&gt;    #define struct union
    #define else
&lt;/code&gt;&lt;/pre&gt;
That&apos;s evil. I have to do it in someone&apos;s code some day just to have some fun.&lt;p&gt;But, apart from that, it&apos;s a really nice compilation. I didn&apos;t know about the compile time checks of array sizes, but I have a doubt. What if I pass to a method declared&lt;p&gt;&lt;pre&gt;&lt;code&gt;    int foo(int x[static 10])
&lt;/code&gt;&lt;/pre&gt;
this pointer&lt;p&gt;&lt;pre&gt;&lt;code&gt;    int* x = (int*) calloc(20, sizeof(int));
&lt;/code&gt;&lt;/pre&gt;
Does the compiler skip the check? Does it give me a warning?&lt;p&gt;EDIT: Funnily enough, in Mac it doesn&apos;t give any warning, neither for pointers nor for undersized arrays (ie, foo(w[5]) doesn&apos;t give a warning). And I&apos;ve compiled with -std=c99 -pedantic -Wall.</text></item></parent_chain><comment><author>emillon</author><text>It&apos;s impossible to have a static decision procedure about dynamic properties of programs, such as the size of dynamically allocated memory areas (Rice&apos;s theorem). So, it is necessary to either include false positives (correct programs rejected) or false negatives (incorrect programs accepted).&lt;p&gt;Sound static analyzers fall in the first case, but require a lot of work to become precise enough to be used (ie, to reduce the number of false alarms). Compilers fall in the second case in the sense that they don&apos;t have to honor such a clause. And in the C99 norm it&apos;s actually a &quot;shall&quot; (it just couldn&apos;t honor a &quot;must&quot; in that case):&lt;p&gt;&quot;If the keyword static also appears within the [ and ] of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as speciﬁed by the size expression.&quot;</text></comment>