5

In standard C, you have a few options for declaring a function that accepts a pointer to a chunk of data:

void style_1(int * arr);

void style_2(int arr[]);

void style_3(int arr[10]);

void style_4(int size, int arr[size]);

void style_5(int arr[static 10]);

void style_6(int size, int arr[static size]);

The thing is, as near as I can tell, most of the "choice" is syntactic and doesn't contribute much to the program's actual meaning. The first three styles appear to be exactly equivalent, because "A declaration of a parameter as 'array of type' shall be adjusted to 'qualified pointer to type'" (C11 6.7.6.3 p7), and style_4 is also the same for the purposes of the pointer parameter. For both style_3 and style_4, the array size is thrown away.

style_5 and style_6 are actually different and impose a useful constraint, guaranteeing a non-null pointer with a minimum amount of storage. But why do we need to add a keyword to do this? The declarations of style_3 and style_4 seem to communicate just as much information to the reader. A compiler would surely have no problem with those, if only the language allowed them to be meaningful? As it is, this design choice seems to introduce a hole that can allow extra errors to slip through, by effectively instructing the machine not to check for them by default.

Why was static added to impose the size constraint on array parameters in C? Why is the size on its own not sufficient?

  • 3
    It doesn't actually impose a constraint (the C standard uses that term to refer to rules whose violation must be diagnosed at compile time, along with syntax rules). Rather if a parameter is declared as int arr[static 10], then passing an argument that doesn't point to the initial element of an array with at least 10 elements has undefined behavior. The point is to allow the compiler to optimize based on the assumption that the argument does properly point to such an object. – Keith Thompson Sep 23 '16 at 0:38
3

For historical reason, in C (and C++) a parameter whose type is written as "array of type T" is actually a "pointer to type T". So int arr [10] just looks like the parameter is an array, in reality it is an int*. The size information is lost.

In C, if you write int arr [static 10] this turns into an int* pointing to 10 elements. Which would allow the compiler to invoke undefined behaviour if your read or write arr[10] or warn you if you assign to arr[10] with a constant 10.

  • I don't understand what is meant by this: "[...] or warn you if you assign to arr [10] with a constant 10.". Redundant with the previous clause? – njuffa Sep 22 '16 at 19:28
  • As per this site, it is claimed that the size must be at least 10; anything less would be flagged as error (I assume compile time checking). – blackpen Sep 22 '16 at 21:56
1

Styles 5 and 6 were only added fairly recently to the C language (in the last decade or so), while the other styles have existed since the beginning for almost 30 years now.

When making changes to the C language, the committee doing so is very reluctant give new meaning to existing syntactic constructs, because it might break some existing code that used to be valid and with well-defined behavior.

That is the reason why styles 5 and 6 were added for the feature of passing an array with a guaranteed minimum size, rather than re-use styles 3 and 4.

  • I have a hard time imagining why any program would intentionally use styles 3 or 4 but not want to fulfil the implied contract. If the program documents the size of the array in the source, but doesn't live up to the documentation, isn't that just a bug? – Leushenko Sep 23 '16 at 18:19
  • @Leushenko: Most developers would indeed classify such a discrepancy as a bug, but according to the C language specification the behavior is perfectly well defined. If the behavior got changed, millions of lines of code would have to be evaluated if they are still correct according to the new specification. That can severely hinder the adoption of the new standard. – Bart van Ingen Schenau Sep 24 '16 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.