I've worked with JavaScript weenies who are in love with the const declaration. They don't have an answer, though, when I ask, "Shouldn't the compiler be able to notice that a variable isn't changed in it's lifetime?" Most of the const values are just local and never leave the function but the weenies continue to stomp their feet when I use "var". The compilers are smart. Let them do their thing.
const doesn't actually stop data from changing, just reassignment. const user = {id: 1, status: 'active'}; user.status = 'disabled'; is valid javascript
Why Const Doesn't Make C Code Faster
71–80 of 197 posts
Re: Why Const Doesn't Make C Code Faster
#72Const variables can also be mapped read-only. This gives you hardware protection against modifications, and also uses less RAM if the const variable is in a shared library (multiple processes can share the same mappings).
Re: Why Const Doesn't Make C Code Faster
#73Re: Why Const Doesn't Make C Code Faster
#74On some microcontrollers, marking variables/arrays as const allows the compiler to access them directly from flash rather than having to copy them into RAM at startup. I used this to great effect on a PIC24 with 256KB flash/16KB RAM.
Re: Why Const Doesn't Make C Code Faster
#75Unfortunately in C++, the compiler is not allowed to assume that if it passes a const reference to a function, that function will not change the object. The function is allowed to cast away const and modify the object. I'm not happy that they did it that way; I would have preferred it if cast-away-const were more restricted (for example, allowed when calling a child function that takes a char* but doesn't modify the…
Correct, but only if the original (referenced) object is not const itself. Casting away constness to modify an object that was declared const is 100% UB.
Re: Why Const Doesn't Make C Code Faster
#76 // x is just a read-only pointer to something that may or may not be a constant
void constFunc(const int *x)
Is that correct? I read that as “x is a pointer to a const int”If you want “x is a read-only pointer to an int”, you would need
void constFunc(int * const x)
The article also doesn’t mention the case “x is a read-only pointer to a constant int”. To state that, you would use void constFunc(const int * const x)
(https://stackoverflow.com/a/1143272)Re: Why Const Doesn't Make C Code Faster
#77Unfortunately in C++, the compiler is not allowed to assume that if it passes a const reference to a function, that function will not change the object. The function is allowed to cast away const and modify the object. I'm not happy that they did it that way; I would have preferred it if cast-away-const were more restricted (for example, allowed when calling a child function that takes a char* but doesn't modify the…
Cast away const. One of the many reasons I gave up on C++. What is const if it can be cast away?
Re: Why Const Doesn't Make C Code Faster
#78Earlier quoted context omitted.
Here's another failure of marketing on my part. We included full library source with the compiler, for free. No reviewer ever noticed that. One day, Borland decided to make source code for their library, sans the floating point stuff, available for an extra charge. This generated headlines in the next compiler roundup article. No mention that complete library source code, including floating point, came with my compil…
By attaching a price to it, did people assume it was higher quality?
With it being packaged separately, people noticed its existence, and did assign value to it commensurate with its price.
It was attractively priced, and almost everyone buying the compiler also bought the library source.
Re: Why Const Doesn't Make C Code Faster
#79Unfortunately in C++, the compiler is not allowed to assume that if it passes a const reference to a function, that function will not change the object. The function is allowed to cast away const and modify the object. I'm not happy that they did it that way; I would have preferred it if cast-away-const were more restricted (for example, allowed when calling a child function that takes a char* but doesn't modify the…
> Unfortunately in C++, the compiler is not allowed to assume that if it passes a const reference to a function, that function will not change the object. how could it make that assumption ? there is nothing that prevents you to implement the function in fortran or even in raw assembly
Now, the tricky part is that const objects (i.e. objects declared as const) enjoy these optimization benefits, but the compiler usually cannot prove that a reference or pointer to const actually refers/points to a const object. Taking a const pointer to a non-const object, casting away the constness again and then modifying the object is totally legal.
Since the former case (objects declared const) is much rarer than the latter case (working on const object pointers/references) the compiler cannot often optimize based on const.
Re: Why Const Doesn't Make C Code Faster
#80Shouldn't the test instead be: const int * const Haven't profiled it but would make more sense to have the pointer also be const instead of having a non const pointer as input?
It rarely makes sense. Pointer to const is a contact between caller and callee. A signature like char strdup(const char ) says "I take a pointer to memory that I promise not to modify, and you get a pointer to memory that you may modify". Const pointer is a statement about the internal variables of a function definition, usually not of any interest outside the function itself and therefore rarely used.
...and in fact not even part of the name mangling (for the exact reason you mentioned): https://godbolt.org/z/1pjecq