Understanding the Const Modifier in C++: Stability, predictability and security
Using const to maintain the integrity of your code and prevent unintended modifications to data.
The following came out of a discussion with ChatGPT. I used to code very basic C++ scripts back in my undergraduate Physics days but I eventually moved on the Python for various practical reasons. Recently, I was learning the basics of coding a server from scratch using C/C++, via the wonderful project written by James Smith. This project gave me a good excuse to get back into C/C++ because I had enjoyed it a lot back then. I became curious about the particular modifiers used with datatypes and my discussion with GPT began with const (note: I have edited to make it less AI-y).
The const modifier in C++ is a type qualifier that is used to specify that a variable or data member's value can’t be changed. The keyword const is used to define a constant data type, for example: const int x = 10. This defines a constant integer variable x and sets it to the value 10. Because we defined it with the constant keyword, we can’t change x unless we redefine it.
Stability and predictability
Once a variable is declared as const, its value cannot be altered throughout the program. The const types are useful for declaring variables whose value is not intended to change, and helps prevent unintended modifications to the data, leading to a more stable and predictable code.
How not to use it
In the following example, message is a const char pointer and its value cannot be changed. The printMessage function takes this pointer as argument and prints out a message (“Hello World” in this case). If we try to modify the value of the message by changing the “H” to a lower case “h”, it will result in a compiler error. This is because the constant qualifier (const) prevents modification of the value of the argument passed to the function.
#include <iostream>
void printMessage(const char *msg) {
std::cout << msg << std::endl;
}
int main() {
const char *message = "Hello World";
printMessage(message);
// The following line will result in a compiler error because
// we can't modify any of the elements of message by definition!
message[0] = 'h';
return 0;
}
Integrity and security
The const modifier can also be applied to pointers and references, granting the programmer the ability to enforce immutability on the memory locations being pointed to. By doing so, the integrity of the data being referred to is preserved and the code becomes more secure. This feature makes it possible to ensure that sensitive data remains unaltered, and prevent accidental or deliberate modifications that could lead to unexpected results or security breaches. Here's a very simple example that illustrates this:
Imagine you have a program that handles bank account numbers and balances. This information could be easily altered by malicious users or by mistakes made by the program itself.
To prevent this, you can declare the variables storing your precious bank information as
const. This way, if a hacker tries to modify your balance or the program messes up somehow, the compiler will throw an error, alerting you to the issue and preventing the modification from happening.
#include <iostream>
int main() {
const int accountNumber = 123456789;
const double accountBalance = 1000.00;
/*
accountNumber and accountBalance are declared as const,
making them read-only and unmodifiable. Attempts to modify
these variables will result in a compiler error, ensuring
the integrity and security of the sensitive financial
information being stored and preventing any unauthorized
transfer of funds.
*/
// This line will result in a compiler error
accountNumber = 223456789; // my account MWAHAHAHA
// another possible exploit if we didn't define const
accountBalance = 9,223,372,036,854,775,807;
// Exercise for the reader: Why this particular number?
std::cout << "Account Number: " << accountNumber << std::endl;
std::cout << "Account Balance: $" << accountBalance << std::endl;
return 0;
}A tangent - Are Python tuples related to const types?
I had this thought upon reading about the immutable nature of const types but I've come to find that while both concepts serve a similar purpose, they are implemented differently (never mind that they come from different programming languages). In Python, tuples are a built-in data type similar to a list, but unlike lists, tuples are immutable, meaning their elements cannot be changed once they are created. In C++, const is used to specify that a variable is read-only and its value cannot be changed during the lifetime of the program.
In the end, I learned that programming in C/C++ involves thinking much deeper about the variables and datatypes that I took for granted coding in Python. I’m kind of obsessed with datatypes now. Hopefully, you too will deeply ponder datatypes (if you don’t already - if so, enlighten me).
Peace.
- K


#include <iostream>
int main() {
std: : << “fantastic article!” << std::endl;
return 0;
}