"hagman" <go****@von-eitzen.dewrites :
jacob navia schrieb:
Problem
You want to ensure that a pointer argument to a function
is non-null.
Solution
int fn(double data[static 1]);
This means that the array (that is passed as a pointer)
must have at least 1 element, i.e. can't be NULL.
I wasn't aware of this till a discussion in comp.std.c
In what way then will
x = fn(p);
fail if p happens to be NULL?
By invoking undefined behavior.
C99 6.7.5.3p7:
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.
This "shall" appears outside a constraint, so if it's violated the
result is undefined behavior.
You might as well just (attempt to) dereference the pointer inside the
function; that invokes UB in exactly the same circumstances. And in
either case, a compiler may, but is not required to, insert a check
(and to decide what happens if the check fails).
--
Keith Thompson (The_Other_Keit h)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.