Note that there are some explanatory texts on larger screens.

plurals
  1. POShould gcc warn when an integral enum that evaluates to 0 is used as a pointer?
    primarykey
    data
    text
    <p>The following program compiles with no warnings (which is undesirable, because omitting the array index on lines 19 and 21 effectively destroys the array). If you compile with -D CHECK_NONZERO, you will see that line 23 will not compile without a warning, as the enum BBB evaluates to 1 where AAA and aaa evaluate to 0.</p> <p>It appears that if an enum evaluates to 0, gcc will seamlessly cast it to a NULL pointer.</p> <p>Should this be considered a bug?</p> <p>EDIT: I don't think I was as clear as I could have been about what I perceive to be the issue. It seems to me there would be no harm in type-checking the enum for warning purposes <em>before</em> resolving the enum to its constant value, but this is not how gcc works at the moment. However, I'm not sure if this is worthy of a bug report or feature request to the gcc project.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; typedef enum { AAA, BBB, } alpha_e; enum { aaa, bbb, }; int main(void) { alpha_e *alpha_array = malloc(sizeof(*alpha_array) * 2); alpha_array[0] = AAA; alpha_array[1] = BBB; printf("1: alpha_array[0] == %u, alpha_array[1] == %u\n", alpha_array[0], alpha_array[1]); alpha_array = AAA; printf("2: alpha_array[0] == %u, alpha_array[1] == %u\n", alpha_array[0], alpha_array[1]); alpha_array = aaa; #ifdef CHECK_NONZERO alpha_array = BBB; #endif return 1; } </code></pre> <p>gcc -v:</p> <pre><code>Using built-in specs. Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5.1' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1) </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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