4

I'm new to C++ and am attempting to figure out what I'm supposed to do.

Is there a difference in performance between the following options:

const DATATYPE* name 

vs

DATATYPE* name 
7
  • 3
    There may be, compiler might have more options to optimize const variables. But that's the least important function of const (and it's unlikely you will notice any difference in performance anyway). Commented Jul 26, 2021 at 8:22
  • 1
    Unless you need to modify the object, always use const. And if you can then please try to avoid pointers at all. Commented Jul 26, 2021 at 8:25
  • 8
    And some nitpicking: It's not the pointer that is constant, it's the object/data being pointed to. This distinction is very important, as you can actually have a constant pointer (i.e. DATATYPE* const) Commented Jul 26, 2021 at 8:25
  • You can try testing the difference in a few cases and check generated assembly with godbolt.org or objdump to see if the compiler has done any optimizations for one over the other. In general though, don't concern yourself with the optimization, use const T * whenever you don't have to modify the dereferenced value, otherwise don't use const. Commented Jul 26, 2021 at 8:28
  • 2
    You seldom need to use pointers directly like that. This has 0 direct effect on codegen (there could be a const_cast to remove the const, the object could be modified through a different pointer, etc). Where it could matter is if calling name->method() ends up calling a different function because of the const. Commented Jul 26, 2021 at 8:44

1 Answer 1

1

In this case there shouldn't actually be any performance gain, because the keyword const in your example only tells the compiler that the pointer access will be limited to read-only access (this could only increase performance e.g. in cases with mutex-protected or atomic pointers). But the pointer itself is going to be a variable!

If, however, you meant to use a constant pointer to a read-only value, then you would have had to declare it like this:

const DATATYPE *const name

And even in this revised case, most compilers shouldn't see any performance improvements, because good compilers automatically detect if something is actually treated as a constant and/or read-only entity in a given code and then optimize accordingly (internally) without the programmer having to bother with redundant and excessive "const embellishments" everywhere. (Not saying all const uses are redunant -- far from it, as e.g. const explicitly used to define a read-only entity makes for a good and safe API.)

Sign up to request clarification or add additional context in comments.

2 Comments

In C++ const is more important than just for readability. For instance, only a const reference can bind to a temporary.
@MSalters, sure, but I never said const is "just for readability". Actually, read the last sentence of my post, please, where I explicitly say "Not saying all const uses are redunant -- far from it". const has many useful roles. But many uses are just pedantic and redundant as noted in official C++ guidelines (yes, they do use the word "pedantic" themselves). Also, note that the guy was asking about performance gains, so we are getting a bit off-topic here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.