In article <I5********@new s.boeing.com>, "Default User" <fi********@boe ing.com.invalid > writes:
Robert Harris wrote:
Default User wrote:
Are some compilers sophisticated enough to be able to detect this
condition and optimize away that strlen() call?
The compiler doesn't necessarily know what your strlen() does; after
all you can write it yourself!
What do you mean? It knows its signature:
size_t strlen(const char *s)
Therefore it knows that it doesn't change s.
The special case of standard functions aside, and the case of casting
away constness aside, this still isn't sufficient to optimize away
the call to a function. The function could have side effects besides
altering its parameters.
The *only* reason why an implementation might be able to optimize
away calls to standard functions is because it knows exactly what
they do. That's the only sufficient guarantee. (Note that this
also requires the restriction against redefining them.)
Of course, an implementation could introduce an extension - such as
a pragma - to denote "pure" functions which have no side effects and
make no use of external state and so could be similarly optimized
away when called repeatedly with the same parameters.[1] However,
standard C has no such facility.
1. Note that a function with no side effects must be a function only
of its input. Maintaining any kind of state between calls would be
a side effect. The restriction against using external state is
necessary to exclude things like clock(), which has no side effects
(except indirectly the one of taking non-zero execution time) but
should not be optimized away, as its return value changes over time.
--
Michael Wojcik
mi************@ microfocus.com
The surface of the word "profession " is hard and rough, the inside mixed with
poison. It's this that prevents me crossing over. And what is there on the
other side? Only what people longingly refer to as "the other side".
-- Tawada Yoko (trans. Margaret Mitsutani)