On Saturday, August 18, 2012 at 5:33:19 AM UTC-7, jacob navia wrote:
In my implementation [of VLA parameters] I had a REAL sizeof that will return the actual
size. I was told to replace it, but I think I will not.
An implementation of sizeof that returns something other than the size of the pointer when given a VLA parameter violates the fundamental rules of C pointer arithmetic. You cannot pass an array to a function in C, except by wrapping a struct or union
around it. Confusingly, C allows you to write function parameters that -appear- to be arrays, e.g.
int main(int argc, char *argv[]) // misleading declaration: argv is NOT an array; rather, it's merely a vector
The compiler knows that an array cannot be passed directly to a function because any time an expression of type array of X appears in expression context (e.g. the actual parameter in a function call), the compiler promotes the type to pointer to X by
taking the address of the first element. So when the compiler sees an array type in a parameter list, it silently re-writes it to what it really is:
int main(int argc, char **argv)
This is a throw-back to pre-ANSI C when the compiler would also silently re-write formal parameters of type "char" and "short" to "int" and "float" to "double". It's too bad ANSI C didn't disallow writing parameters that appear incorrectly as array
types, but the fact that they didn't made VLA parameters possible.
In my example, regardless of which declaration I use for argv, argv is a pointer in every way. For example, we can say ++argv. We wouldn't be able to increment it if it were an array.
The VLA parameter is similarly silently rewritten as a pointer type, and just as above, when we have:
void f(int n, int a[n])
We can say ++a inside the function.
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)