HOWTO: const and pointer variants in C and C++

A

Adem

HOWTO: const and pointer variants in C and C++ :

void test()
{
int n = 1; // n is non-const data of type int
++n; // ok

const int c = 2; // c is const data of type int
// ++c; // err

// Case1:
int* p1 = &n; // p1 is a non-const ptr to non-const data
++p1; // ok
*p1 += 3; // ok

// int* px = &c; // err

// Case2:
int* const p2 = &n; // p2 is a const ptr to non-const data
// ++p2; // err
*p2 += 3; // ok

// Case3:
const int* p3 = &c; // p3 is a non-const ptr to const data
++p3; // ok
// *p3 += 3; // err

// Case4:
const int* const p4 = &c; // p4 is a const ptr to const data
// ++p4; // err
// *p4 += 3; // err

// Case5:
const int* const p5 = &n; // Similar to Case4 but here non-const n is treated as const data when accessed via p5
// ++p5; // err
// *p5 += 3; // err

// Case6:
const int *const p6 = &c; // same as Case4
// ++p6; // err
// *p6 += 3; // err

// Case7:
const int *const p7 = &n; // Same as Case5
// ++p7; // err
// *p7 += 3; // err

// const* int p8 = &n; // err
// const* int p9 = &c; // err

// const int const* pa = &n; // err: duplicate 'const'
// const int const* pb = &c; // err: duplicate 'const'
}

For safety and speed you should use const whenever possible.
 
A

Andrey Tarasevich

blargg said:
const could help a compiler determine if a variable
could be replaced with a constant in machine code.
Opcodes with operands, usually have smaller faster
versions which operate on constants.
[...]

Yes, but can you provide EXAMPLES of where const actually helps the
compiler determine this, where it couldn't determine it without const?

'const' in C++ has serves multiple purposes. It can 1) declare constness
of an object itself, and 2) declare constness of an access path to an
object, 3) serve as a distinguishing trait in overload resolution.

How the third role can be used in "optimization" is rather obvious. But
these are basically user optimizations, as opposed to complier
optimizations. I assume that you are talking about compiler
optimizations, to which the third role is not immediately relevant.

I don't think I need to provide examples of how the first role of
'const' can speed up the code. They are immediately obvious as well.
Constant values can be calculated at compile time. Code that depends on
constant values can be reduced at compile time. Etc.

The second role of 'const' is more about discipline, than about
optimization. In general case, the constness of access path does not
imply the constness of the object this access path leads to. Which means
that in general case without preforming a thorough aliasing analysis,
the compiler can't do much based on the constness of access path itself.
And if the compiler is smart enough to perform a sufficiently thorough
aliasing analysis, it should be smart enough to optimize it regardless
of whether the explicit 'const' is supplied by the user or not. So, I'd
agree that the second role of 'const' by itself is not immediately
useful for compiler optimizations. However, it is worth noting that it
might become quite useful if the user explicitly gives the compiler the
permission to perform "overly agressive" optimizations (meaning, that
they might be formally invalid in general case) by assuming some
aliasing properties of the code, instead of trying to derive them from
the code itself. (I'm not sure, but it is quite possible that the
combination of 'const' with C99's 'restrict' allows for more valid
straightforward optimizations that just a mere 'restrict'.)
 
J

James Kuyper

pete said:
Hendrik said:
pete said:
[...]
I don't think that removing const
ever changes the semantics of a program.

struct test {
int f() {return 1;}
int f() const {return 0;}
};

int main()
{
/*const*/ test x;
return x.f();
}

In order to make a point,
you would have to post a correct c program

In case you haven't noticed, this message is cross-posted to two groups
where C++ code is entirely appropriate; the Subject: line might also be
a hint.
which contains the const keyword,
and which also does something different when const is removed.

The above program does something quite different when the comment
markers are removed from around the second 'const'.
 
F

Flash Gordon

Hendrik Schober wrote, On 11/11/08 15:41:
pete said:
[...]
I don't think that removing const
ever changes the semantics of a program.

struct test {
int f() {return 1;}
int f() const {return 0;}
};

This is not valid C and is an example of why cross-posting between C and
C++ groups is generally a bad idea. Please keep answers which are
specifically C++ in groups where C++ is topical.
 
F

Flash Gordon

James Kuyper wrote, On 11/11/08 19:12:
pete wrote:


In case you haven't noticed, this message is cross-posted to two groups
where C++ code is entirely appropriate; the Subject: line might also be
a hint.

It is also posted to a group where C++ is NOT topical. The way pete
indicated this might not be the best, but it is certainly true that the
example given was not correct for comp.lang.c where only C is topical.
Is it really too difficult for people with C++ specific answers to drop
the cross-post to comp.lang.c?
 
A

Andrey Tarasevich

Flash said:
Is it really too difficult for people with C++ specific answers to drop
the cross-post to comp.lang.c?

Dropping is not difficult. _Noticing_ might be. I, for one, use
Thunderbird and most of the time don't notice that I post to multiple
newsgroups at once.
 
F

Flash Gordon

Pete Becker wrote, On 11/11/08 20:24:
It's no more difficult than for people with comments about the
appropriateness of postings in comp.lang.c to drop the cross-post to
comp.lang.c++.

I had not registered that it was James Kuyper, who does read
comp.lang.c, who made the comment above about it being topical in two of
the groups this is cross-posted to. Sorry. If I had registered I would
have dropped the cross-post.

In general where threads are cross-posted between comp.lang.c and
comp.lang.c++ it is people who don't read comp.lang.c who post messages
cross-posted to it which are not topical "here", so in general making
such a comment just in comp.lang.c can't do any good. People in all
three of these groups (including comp.lang.c) need to watch out for
cross-posts and drop comp.ang.c++ where it is a C specific answer, and
comp.lang.c where it is a C++ answer, and tell the OP what a bad idea
the cross-post is because of the problems it causes.
 
A

Antoninus Twink

People in all three of these groups (including comp.lang.c) need to
watch out for cross-posts and drop comp.ang.c++ where it is a C
specific answer, and comp.lang.c where it is a C++ answer,

You might /like/ people to do this for your own political reasons. But
they don't /need/ to. They can do whatever they think is appropriate,
whether it suits your prejudices or not.
and tell the OP what a bad idea the cross-post is because of the
problems it causes.

There is no problem - you just choose to believe there is for your own
political reasons.
 
J

James Kuyper

Flash said:
Pete Becker wrote, On 11/11/08 20:24:

I had not registered that it was James Kuyper, who does read
comp.lang.c, who made the comment above about it being topical in two of
the groups this is cross-posted to. Sorry. If I had registered I would
have dropped the cross-post.

I think that a comparison between C and C++ with regard to 'const' and
pointers should be on-topic in all three groups, but that everyone
involved in such a discussion should be careful to distinguish which
language they're talking about at any given point in the discussion,
except when making a statement that they believe to be true in both
languages.
 
J

James Kanze

const could help a compiler determine if a variable could be
replaced with a constant in machine code. Opcodes with
operands, usually have smaller faster versions which operate
on constants.

Yes, but can you provide EXAMPLES of where const actually
helps the compiler determine this, where it couldn't determine
it without const?

It's generally true of top-level const. Something like:

int const i = 42 ;

, for example. The compiler knows that regardless of what
happens elsewhere in a legal program, i will always be equal to
42.
About the only one is a non-static variable at file scope,
where the compiler can't in general assume its value after
code in other translation units has executed, unless it's
declared const. A variantis a local const object whose address
is passed to a function; removing const would prevent the
compiler from assuming its value doesn't change during the
function call. I contend that these are rare cases.

In C, probably, since such variables can't be used in constant
expressions. In C++, it's actually quite common.
 
O

Old Wolf

    const  int*       p3 = &c;  // p3 is a non-const ptr to const data

Not exactly, e.g. :
const int *p3 = &n; // not error


Also, you omitted:
int const *p
int const *const p
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top