By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,657 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,657 IT Pros & Developers. It's quick & easy.

constants - pp vs. const

P: n/a
I remember reading somewhere that C's 'const' keyword is almost useless,
except for maybe triggering some additional compiler warnings. 'Useless' as
in: a conforming C compiler cannot assume that a variable declared const is
actually constant (never modified), because it is valid to cast the const
modifier away e.g.

const int foo = 2;
int *bar;

bar = (int *)&foo;

*bar = 4;

Is that correct?

Jun 11 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a

"copx" <co**@gazeta.plschrieb im Newsbeitrag
news:f4**********@inews.gazeta.pl...
>I remember reading somewhere that C's 'const' keyword is almost useless,
[snip]

Sorry, wrong header. I originally intended to post a different question and
forgot to change the subject.
Jun 11 '07 #2

P: n/a
copx wrote On 06/11/07 11:10,:
I remember reading somewhere that C's 'const' keyword is almost useless,
except for maybe triggering some additional compiler warnings. 'Useless' as
in: a conforming C compiler cannot assume that a variable declared const is
actually constant (never modified), because it is valid to cast the const
modifier away e.g.

const int foo = 2;
int *bar;

bar = (int *)&foo;

*bar = 4;

Is that correct?
No, any attempt to modify a const-qualified variable
produces undefined behavior. As a practical matter, you
will find that some compilers allocate const-qualified
variables in read-only memory.

As for the "almost useless" part, I refuse to take
part in empty-headed debates with scurrilous morons who
use loaded terms to advance their impious agendas.

--
Er*********@sun.com
Jun 11 '07 #3

P: n/a
"copx" <co**@gazeta.plwrote:
I remember reading somewhere that C's 'const' keyword is almost useless,
except for maybe triggering some additional compiler warnings. 'Useless' as
in: a conforming C compiler cannot assume that a variable declared const is
actually constant (never modified), because it is valid to cast the const
modifier away e.g.

const int foo = 2;
int *bar;

bar = (int *)&foo;

*bar = 4;

Is that correct?
Not quite. Doing so causes undefined behaviour. A C implementation _is_
allowed to assume that a const _object_ remains constant. What you're
thinking of is the other end of the equation: given

const int *bar;

an implementation cannot assume that the object at bar will remain
constant, because bar can be pointed at an object that itself is not
const. IOW, _pointers to_ const types are not as useful as they could
be. However, they're still very useful for function parameters; for
example, given the declaration

void func(const int *baz);

both the implementation and the maintenance programmer can rely on
func() not changing the object that baz points at, at least not through
baz, and provided func() is well written. This is frequently used in the
Standard library; for example, strcpy() is declared

char *strcpy(char *s1, const char *s2);

and this means that if you call strcpy() correctly, you can rely on it
never modifying the second string you pass it, whatever it does to the
first string. (In C99, restrict is used to tighten both that "if" and
the resulting guarantee.)

Richard
Jun 11 '07 #4

P: n/a

"Richard Bos" <rl*@hoekstra-uitgeverij.nlschrieb im Newsbeitrag
news:46****************@news.xs4all.nl...
"copx" <co**@gazeta.plwrote:
>I remember reading somewhere that C's 'const' keyword is almost useless,
except for maybe triggering some additional compiler warnings. 'Useless'
as
in: a conforming C compiler cannot assume that a variable declared const
is
actually constant (never modified), because it is valid to cast the const
modifier away e.g.

const int foo = 2;
int *bar;

bar = (int *)&foo;

*bar = 4;

Is that correct?

Not quite. Doing so causes undefined behaviour. A C implementation _is_
allowed to assume that a const _object_ remains constant. What you're
thinking of is the other end of the equation: given

const int *bar;

an implementation cannot assume that the object at bar will remain
constant, because bar can be pointed at an object that itself is not
const.
[snip]

Thank for you for clearing this up.

Jun 11 '07 #5

P: n/a

"Eric Sosman" <Er*********@sun.comschrieb im Newsbeitrag
news:1181575088.964548@news1nwk...
[snip]
No, any attempt to modify a const-qualified variable
produces undefined behavior. As a practical matter, you
will find that some compilers allocate const-qualified
variables in read-only memory.
Ok, thanks.
As for the "almost useless" part, I refuse to take
part in empty-headed debates with scurrilous morons who
use loaded terms to advance their impious agendas.
Loaded terms like "useless" as opposed to technical terms like "scurrilous
morons", I guess :P
Jun 11 '07 #6

P: n/a
copx wrote:
"Eric Sosman" <Er*********@sun.comschrieb im Newsbeitrag
news:1181575088.964548@news1nwk...
> As for the "almost useless" part, I refuse to take
part in empty-headed debates with scurrilous morons who
use loaded terms to advance their impious agendas.

Loaded terms like "useless" as opposed to technical terms like "scurrilous
morons", I guess :P
SWOOSH!

Jun 11 '07 #7

P: n/a
Richard Bos <rl*@hoekstra-uitgeverij.nlwrote:
A C implementation _is_ allowed to assume that a const _object_ remains
constant.
ITYM "program" rather than "implementation". The "as-if" rule
applies, of course.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jun 11 '07 #8

P: n/a
Christopher Benson-Manica <at***@vinland.freeshell.orgwrites:
Richard Bos <rl*@hoekstra-uitgeverij.nlwrote:
>A C implementation _is_ allowed to assume that a const _object_ remains
constant.

ITYM "program" rather than "implementation". The "as-if" rule
applies, of course.
No, I think he meant implementation. More specifically, an
implementation is allowed to assume that a const object is not
modified, and to generate code that depends on that assumption.

For example, given:

#include <stdio.h>
int main(void)
{
const int x = 42;
*(int*)&x = 123;
printf("x = %d\n", x); /* line 6 */
return 0;
}

the compiler is allowed to replace the reference to x on line 6 with a
literal 42. At least one compiler actually does this if you request
optimization.

--
Keith Thompson (The_Other_Keith) 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"
Jun 11 '07 #9

P: n/a
copx wrote:
>
I remember reading somewhere that C's 'const' keyword is almost
useless, except for maybe triggering some additional compiler
warnings. 'Useless' as in: a conforming C compiler cannot assume
that a variable declared const is actually constant (never
modified), because it is valid to cast the const modifier away
e.g.

const int foo = 2;
int *bar;

bar = (int *)&foo;

*bar = 4;

Is that correct?
Correct, but foolish. Use it properly.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 11 '07 #10

P: n/a
CBFalconer <cb********@yahoo.comwrites:
copx wrote:
>I remember reading somewhere that C's 'const' keyword is almost
useless, except for maybe triggering some additional compiler
warnings. 'Useless' as in: a conforming C compiler cannot assume
that a variable declared const is actually constant (never
modified), because it is valid to cast the const modifier away
e.g.

const int foo = 2;
int *bar;

bar = (int *)&foo;

*bar = 4;

Is that correct?

Correct, but foolish. Use it properly.
No, it's not correct. Attempting to modify a const-qualified object
invokes undefined behavior.

--
Keith Thompson (The_Other_Keith) 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"
Jun 11 '07 #11

P: n/a
Keith Thompson <ks***@mib.orgwrote:
Richard Bos <rl*@hoekstra-uitgeverij.nlwrote:
A C implementation _is_ allowed to assume that a const _object_ remains
constant.
No, I think he meant implementation. More specifically, an
implementation is allowed to assume that a const object is not
modified, and to generate code that depends on that assumption.
On another reading, I think you're right, but the phrasing still seems
a bit unclear to me. Just another Monday I suppose.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jun 12 '07 #12

P: n/a
Le 11-06-2007, copx <co**@gazeta.pla écrit*:
I remember reading somewhere that C's 'const' keyword is almost useless,
except for maybe triggering some additional compiler warnings. 'Useless' as
in: a conforming C compiler cannot assume that a variable declared const is
actually constant (never modified), because it is valid to cast the const
modifier away e.g.

const int foo = 2;
int *bar;

bar = (int *)&foo;
You are lying to the compiler.
Try
char tab[9]={0,1,2,3,4,5,6,7,8};
double *d= (double*) (tab+1);
what happen on your platform ?
*bar = 4;

Is that correct?
No. Writing in a const variable is an undefined behavior. In
a PC-like environment, it could be as if the variable was not
const, but the compiler is also allowed to put the const varible
in a read-only memory.

Marc Boyer
Jun 12 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.