Read-only functionality without 'const'

R

Richard Tobin

put the value in read-only memory, because there is no defined way to
modify it.
[/QUOTE]
Around this point revolves my query.
What does this read-only memory mean here ?
What does it refer to ? Is it a portion of stack or heap or something
else ?
Where is it ?

Typically it would be a segment of memory marked as unwritable by the
operating system (as, for example, the program code usually is). The
operating system does this by setting some bit in thte page
descriptor, which is interpreted by the processor as meaning "don't
allow writes to this page".

In principle it could be some kind of ROM on an embedded system.

Both of these are only likely for const variables initialised to
compile-time constants, because otherwise it would have to make
the memory writable to store the constant value.

-- Richard
 
K

karthikbalaguru

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 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.

Thx,
Karthik Balaguru.
 
S

santosh

karthikbalaguru said:
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'.

restrict has no connection with const. They are both for different purposes.
restrict is used to permit the compiler to optimise certain operations which
it would otherwise not be able to do, due to the possibility of aliasing.

It's extensively used by standard library functions.
 
K

Keith Thompson

I wrote the above. Please leave attribution lines in place for any
quoted text (such as the line above that says "karthikbalaguru
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>.)
 
C

CBFalconer

karthikbalaguru said:
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.

I think you have things mixed up. 'restrict' signals the called
routine that the parameters are totally independent, i.e. do not
overlap. It does not enforce that condition. Its primary purpose
is to enable more aggressive optimization.

Check the C standard.
 
K

karthikbalaguru

I wrote the above. Please leave attribution lines in place for any

easier to follow the discussion.


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_Keith) (e-mail address removed) <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"

The link of the latest c99 draft clarified many of my doubts.
Thx for the info .

Karthik Balaguru
 
K

Keith Thompson

karthikbalaguru said:
The link of the latest c99 draft clarified many of my doubts.
Thx for the info .

You're welcome.

When you post a followup, *please* take the time to delete parts of
the previous article that aren't directly relevant to your response.
In particular, don't quote the signature (the stuff following the
"-- " line) unless you're actually commenting on it. See most
of the followups posted here for examples.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top