Objectmix
Tags Register Mark Forums Read

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

This is a discussion on HOWTO: const and pointer variants in C and C++ within the c++ forums in Programming Languages category; 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 ...


Object Mix > Programming Languages > c++ > HOWTO: const and pointer variants in C and C++

c++ comp.lang.c++ usenet archive

Reply

 

LinkBack Thread Tools
  #1  
Old 11-11-2008, 05:19 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default HOWTO: const and pointer variants in C and C++

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.

Reply With Quote
  #2  
Old 11-11-2008, 09:02 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: HOWTO: const and pointer variants in C and C++

Adem wrote:
> 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


I hate to be pedantic, but this line isn't an error since it's commented out.

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


No, that's undefined behavior because you're dereferencing a past-the-end
pointer. This isn't, though:

int* p1 = &n;
++p1;
--p1;
*p1 += 3;

[...]
> For safety and speed you should use const whenever possible.


Care to show any examples of where const helps speed-wise? That is, a
program where removing const wouldn't change the semantics of the program,
but would reduce its speed. I'm not arguing against using const, just
questioning this often-made claim.
Reply With Quote
  #3  
Old 11-11-2008, 09:02 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: HOWTO: const and pointer variants in C and C++

Adem wrote:
> 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


I hate to be pedantic, but this line isn't an error since it's commented out.

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


No, that's undefined behavior because you're dereferencing a past-the-end
pointer. This isn't, though:

int* p1 = &n;
++p1;
--p1;
*p1 += 3;

[...]
> For safety and speed you should use const whenever possible.


Care to show any examples of where const helps speed-wise? That is, a
program where removing const wouldn't change the semantics of the program,
but would reduce its speed. I'm not arguing against using const, just
questioning this often-made claim.
Reply With Quote
  #4  
Old 11-11-2008, 09:36 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: HOWTO: const and pointer variants in C and C++

blargg wrote:

> Care to show any examples of where const helps speed-wise? That is, a
> program where removing const wouldn't change the semantics of the program,
> but would reduce its speed. I'm not arguing against using const, just
> questioning this often-made claim.


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.

On general principle,
I prefer to give the compiler whatever information I can.

I don't think that removing const
ever changes the semantics of a program.
Assuming that by that, you mean to start with a correct program
which happens to use the const keyword, and then remove the keyword.

--
pete
Reply With Quote
  #5  
Old 11-11-2008, 09:36 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: HOWTO: const and pointer variants in C and C++

blargg wrote:

> Care to show any examples of where const helps speed-wise? That is, a
> program where removing const wouldn't change the semantics of the program,
> but would reduce its speed. I'm not arguing against using const, just
> questioning this often-made claim.


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.

On general principle,
I prefer to give the compiler whatever information I can.

I don't think that removing const
ever changes the semantics of a program.
Assuming that by that, you mean to start with a correct program
which happens to use the const keyword, and then remove the keyword.

--
pete
Reply With Quote
  #6  
Old 11-11-2008, 10:34 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: HOWTO: const and pointer variants in C and C++

pfiland@mindspring.com wrote:
> blargg wrote:
> > Care to show any examples of where const helps speed-wise? That is, a
> > program where removing const wouldn't change the semantics of the program,
> > but would reduce its speed. I'm not arguing against using const, just
> > questioning this often-made claim.

>
> 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?
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.
Reply With Quote
  #7  
Old 11-11-2008, 10:34 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: HOWTO: const and pointer variants in C and C++

pfiland@mindspring.com wrote:
> blargg wrote:
> > Care to show any examples of where const helps speed-wise? That is, a
> > program where removing const wouldn't change the semantics of the program,
> > but would reduce its speed. I'm not arguing against using const, just
> > questioning this often-made claim.

>
> 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?
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.
Reply With Quote
  #8  
Old 11-11-2008, 10:41 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: HOWTO: const and pointer variants in C and C++

pete wrote:
> [...]
> 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();
}

> [...]


Schobi
Reply With Quote
  #9  
Old 11-11-2008, 10:41 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: HOWTO: const and pointer variants in C and C++

pete wrote:
> [...]
> 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();
}

> [...]


Schobi
Reply With Quote
  #10  
Old 11-11-2008, 01:37 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: HOWTO: const and pointer variants in C and C++

blargg wrote:
> pfiland@mindspring.com wrote:
>> blargg wrote:
>>> Care to show any examples of where const helps speed-wise? That is, a
>>> program where removing const wouldn't change the semantics of the program,
>>> but would reduce its speed. I'm not arguing against using const, just
>>> questioning this often-made claim.

>> 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 ****ysis,
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 ****ysis, 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'.)

--
Best regards,
Andrey Tarasevich
Reply With Quote
Reply

Thread Tools



All times are GMT -5. The time now is 03:13 PM.