News for hackers. Stuff that matters.
Re:A nice, little known C feature: Static array indices in p... by Nav_Panel on Monday February 18, 2013 @12:43PM (#5239461)
Doesn't seem to work for me using GCC 4.7.2.

flags: -g -Wall -Wextra -std=c99 -pedantic

The following code compiles and runs (for me at least) with no errors. Tried with both stack and heap allocated arrays of various sizes.

    #include <stdlib.h>
    
    void foo(int array[static 10]) {
      (void) array; // suppress unused var compiler warning
      return;
    }
    
    int main () {
      int *x = calloc(10, sizeof(int));
      int *y = calloc(9, sizeof(int));
      int *z = calloc(11, sizeof(int));
      foo(x);
      foo(y);
      foo(z);
      foo(NULL);
      int a[9];
      int b[4];
      int c[11];
      foo(a);
      foo(b);
      foo(c);
      return 0;
    }
Re:A nice, little known C feature: Static array indices in p... by ambrop7 on Monday February 18, 2013 @12:51PM (#5239499)
I've tried this, and gcc indeed doesn't produce warnings even with -Wall -Wextra. Clang does however without needing any flags.
Re:A nice, little known C feature: Static array indices in p... by qznc on Monday February 18, 2013 @02:54PM (#5240232)
My clang (3.0-6ubuntu3) does not.
Re:A nice, little known C feature: Static array indices in p... by jrajav on Monday February 18, 2013 @02:30PM (#5240116)
I can confirm. Perhaps this is a bug? As per the C99 standard [Clause 6.7.5.3: Function Declarators, point 7]:

A declaration of a parameter as "array of type" shall be adjusted to "qualified pointer to type", where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation. 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 specified by the size expression.

Since that's a 'shall' declaration, shouldn't it at least throw out a warning?

Re:A nice, little known C feature: Static array indices in p... by quasive on Monday February 18, 2013 @03:15PM (#5240353)
This "shall" is not listed under "Constraints", so violation of it is undefined behavior and does not require a diagnostic. See 4 (Conformance): If a ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of a constraint is violated, the behavior is undefined.

This is an area where a compiler can (sometimes) see violations, though, so I think gcc should diagnose when possible, in the same vein as format specifier mismatches in printf().

Re:A nice, little known C feature: Static array indices in p... by onedognight on Tuesday February 19, 2013 @01:38PM (#5246119)
> The information provided by static in parameter array declarators is not used for optimization.

This is not a reason to avoid the construct, but as yet gcc[1] doesn't optimize it either.

[1] http://gcc.gnu.org/c99status.html

Re:A nice, little known C feature: Static array indices in p... by niggler on Monday February 18, 2013 @01:11PM (#5239608)
Not in front of a computer, but have you tried -std=gnu99
Re:A nice, little known C feature: Static array indices in p... by DiabloD3 on Monday February 18, 2013 @01:21PM (#5239648)
Thats a bad way of writing code, to be honest. On projects I run, code has to be warning free with -Wall -Wextra -pedantic -std=c99
Re:A nice, little known C feature: Static array indices in p... by kelnos on Monday February 18, 2013 @03:49PM (#5240548)
It's not bad at all, at least if you've made the conscious decision to write GNU C and not std C, and accept that non-gcc compilers (except maybe clang) may not be able to compile your code.

Unfortunately, though, I believe one of the -std=gnuXX variants is the default, so most people don't make that a conscious decision.

Re:A nice, little known C feature: Static array indices in p... by niggler on Monday February 18, 2013 @03:58PM (#5240591)
gnu89 is the default. gnu99 will be the default when C99 is fully implemented (from the manpage, search for /-std=$/)
Re:A nice, little known C feature: Static array indices in p... by simias on Tuesday February 19, 2013 @03:52AM (#5243351)
C99 is still not fully implemented in GCC? What's missing?
Re:A nice, little known C feature: Static array indices in p... by TorKlingberg on Tuesday February 19, 2013 @09:11AM (#5244323)
Re:A nice, little known C feature: Static array indices in p... by Nav_Panel on Monday February 18, 2013 @01:44PM (#5239809)
Just tried it now. Still compiles/runs without errors.