s***@gmail.com
2016-06-12 18:51:47 UTC
What I'm referring is the confusing new syntax which allow constructs like this:
void f(int p[static 6]) { }
Not only that declaration of a parameter as ‘‘array of type’’ is confusing enough but now we decided to bloat it with additional keywords and new meanings with the intend of allowing some fishy "optimizations".
First if those optimizations were so important why limit ourselves with only array parameters - why don't we allow similar information to be added for all other declarations.
Though to be honest I don't think any new syntax is needed anyway because we already have similar construct in the language called "array types". If we wanted to restrict the passed array as of specific number of elements we could instead declare the parameter as "pointer to array" which will not only provide the compiler with usable information for possible optimizations but it'll also provide usable static type information which could be used to diagnose function calls with incompatible argument types. I.e.:
void f(int (*p)[6]) { }
Of-course this solution won't be available when targeting compatibility with old code calling a function declared with parameter of type "pointer to T" as T being the type of elements of the passed array.
In this case though there is still a way to give the compiler similar information by simply casting the received pointer of type "pointer to T" to "pointer to array of N elements with type T" inside the function as N being constant-expression. For example:
void f(int p[6]) { } //old version with no available size information for the //pointed array to the optimizer
void f(int *p) { p = *((int (*)[6])p); } //size information of the pointed array //can be retrieved by the array from which 'p' is updated
Now as long as you don't make any copies of p previous the assignment - the compiler will presumably know the size of the array which it points by first element.
I know that there is a possibility of the code above to produce UB but chances are that conversion from "pointer to T" to "pointer to array of T" will be legal on all implementations as arrays are so limited in C anyway.
Though my biggest question here is why weren't array parameters made obsolescent considering their confusing and inconsistent behavior (compared to other array declarations).
void f(int p[static 6]) { }
Not only that declaration of a parameter as ‘‘array of type’’ is confusing enough but now we decided to bloat it with additional keywords and new meanings with the intend of allowing some fishy "optimizations".
First if those optimizations were so important why limit ourselves with only array parameters - why don't we allow similar information to be added for all other declarations.
Though to be honest I don't think any new syntax is needed anyway because we already have similar construct in the language called "array types". If we wanted to restrict the passed array as of specific number of elements we could instead declare the parameter as "pointer to array" which will not only provide the compiler with usable information for possible optimizations but it'll also provide usable static type information which could be used to diagnose function calls with incompatible argument types. I.e.:
void f(int (*p)[6]) { }
Of-course this solution won't be available when targeting compatibility with old code calling a function declared with parameter of type "pointer to T" as T being the type of elements of the passed array.
In this case though there is still a way to give the compiler similar information by simply casting the received pointer of type "pointer to T" to "pointer to array of N elements with type T" inside the function as N being constant-expression. For example:
void f(int p[6]) { } //old version with no available size information for the //pointed array to the optimizer
void f(int *p) { p = *((int (*)[6])p); } //size information of the pointed array //can be retrieved by the array from which 'p' is updated
Now as long as you don't make any copies of p previous the assignment - the compiler will presumably know the size of the array which it points by first element.
I know that there is a possibility of the code above to produce UB but chances are that conversion from "pointer to T" to "pointer to array of T" will be legal on all implementations as arrays are so limited in C anyway.
Though my biggest question here is why weren't array parameters made obsolescent considering their confusing and inconsistent behavior (compared to other array declarations).