is there any performance benefit of using constant pointer to constant variable like the following?

void doSome(const int * const param)
{
//do something ;
}

i know that making param constant is good for performance, how about making both constant?

Recommended Answers

All 4 Replies

I don't think that would help the performance. If anything, it would just increase build time by about a few clock cycles (setting a const flag). If you actually broke down the code you write into assembly you'll see that const isn't used, it only means something to us as developers.

I don't think that would help the performance. If anything, it would just increase build time by about a few clock cycles (setting a const flag). If you actually broke down the code you write into assembly you'll see that const isn't used, it only means something to us as developers.

i think it has to do with stack and heap thing but i dont know the details..

It really depends on what you want to achieve.

// disallow param = something and *param = something 
void doSome(const int * const param)

// param can be changed, but what it points to cannot
void doSome(const int * param)

// you can't change param, but *param = something is fine
void doSome( int * const param)

// open season on any assignment
void doSome(int * param)

It's up to you to decide how much const correctness you want to have in your code.

But beware that a partially const correct body of code can be a real nightmare to work with, particularly where const-correct code makes calls to code which isn't.

It's easiest to read it from right to left. int const * const reads constand pointer to constant integer. Meaning the pointer value won't change, and the pointed to value won't change. No effect on performance really, since it is build into the compiler to look for const conflicts before the code is made into machine code. After that the flags are totally dropped (there's no room for a flag in a 32-bit integer) therefore make no difference once compiled.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.