karthikbalaguru <ka************ ***@gmail.comwr ites:
>Remember that "const" doesn't really mean "constant"; it merely means
"read-only". For example, 42 is a constant, but:
const int x = 42;
x is not a constant; it's merely read-only. (But the compiler can
choose to store x in read-only memory, or not store it at all if its
address is never used; nevertheless, x can't be used where a constant
expression is required.)
I wrote the above. Please leave attribution lines in place for any
quoted text (such as the line above that says "karthikbalagur u
<ka************ ***@gmail.comwr ites:"). They make it substantially
easier to follow the discussion.
I came across 'restrict' keyword that is very interesting to share
with you here.
I find that the 'restrict' keyword overcomes the drawbacks of 'const'.
Using 'restrict' keyword, all data accessed through it will be
accessed only through that but not through any other methods.
Any comments/fireballs w.r.t this are welcome.
The 'restrict' keyword is new in C99, so many compilers may not (yet?)
support it.
Unlike 'const', it's really just an optimization hint. By using it,
your not asking the compiler to enforce any restriction; instead,
you're promising the compiler that *you* won't violate the
restriction. The compiler is then free to assume that you haven't
violated your promise, which allows it to perform certain
optimizations; if you break your promise, either deliberately or
accidentally these optimizations can break your code.
C99 6.7.3p7:
An object that is accessed through a restrict-qualified pointer
has a special association with that pointer. This association,
defined in 6.7.3.1 below, requires that all accesses to that
object use, directly or indirectly, the value of that particular
pointer. The intended use of the restrict qualifier (like the
register storage class) is to promote optimization, and deleting
all instances of the qualifier from all preprocessing translation
units composing a conforming program does not change its meaning
(i.e., observable behavior).
See also the formal description in C99 6.7.3.1. (The latest C99 draft
is available at
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf>.)
--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"