This is the mail archive of the
gsl-discuss@sources.redhat.com
mailing list for the GSL project.
Re: Parameter vectors declared as const in minimized functions
On Thu, Jan 06, 2005 at 09:37:45AM -0500, Robert G. Brown was heard to remark:
> where the calling routine needs to be certain that
> the parameters it passed to the function aren't changed in mid-loop.
const has a two-fold purpose:
-- it allows the compiler or other tools to statically check for
various programming errors. For example, the Linux kernel is
heavily marked up with this and a a half-dozen other attributes,
to make sure that kernel pointers aren't passed to user space, and etc.
There are several checkers incl the stanford checker, and
hundreds of (potential) bugs were found and fixed this way.
Computer security types are quite interested in the sort of
static analysis tools this allows.
-- it allows the compiler to optimize leaf calls, tail recursive calls,
etc.
> Just as a matter of curiosity (and because I truly do believe that RTFM
> is an excellent first step to learning) where is this documented --
> preferrably online?
The C99 ANSI standard. It'll will put you to sleep, though.
http://www.nirvani.net/docs/ansi_c.pdf
> int j,k;
> int f(const int *arg)
> {
> if(check(arg,k)){
> j = *arg - 1;
> k = *arg + 1;
> } else {
> j = *arg + 2;
> k = *arg - 1;
> }
> }
Because f() accesses globals, the compiler can no longer assume that
it is a leaf node without side-effects. Thus, if a call to f() appears
inside of a loop, it can no longer be automatically moved out of the
loop.
--linas