Saturday, May 9, 2009

Describe usage of const in C++?

In C++, const is used to declare a constant. A constant is similar to a variable in the sense that it represents a memory location. It differs, as I sure you can guess, in that cannot be reassigned a new value after initialization. In general, constants are a useful feature that can prevent program bugs and logic errors. Unintended modifications are prevented from occurring. The compiler will catch attempts to reassign constants new values

Describe usage of const in C++?
There are two general reasons to use "const" - neither of which is mandatory - your code will work quite well without them.





The first reason is primarily of importance in embedded systems - those computers that are put into cars, aircraft and photocopy machines. These computers have ROM (Read Only Memory) for constants and for the program itself, and RAM (Random Access Memory) for stack, variables and things that can change - they typically do not have disk drives.





Now as a rule of thumb, ROM is cheap, RAM is expensive - you always try to minimize the use of RAM. Thus if you declare an array of constants that are guaranteed not to change - for example consider the following:





char *Days[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};





being of course, the days of the week. These clearly are constants that will never change - you won't get new days of the week. If we make the above declaration, the compiler will create a copy of the strings, and store it in ROM and when the program starts, it will make a copy of them into a RAM location, since it does not know that you are not going to try to change them.





If instead we make the following declaration:





const char *Days[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};





the compiler now knows that these are only constants and will never change. The compiler is able to put these into ROM and never copy them to RAM, since they are constants.





You will have just saved some precious RAM and earned the admiration of your peers.





This usage is of little importance in programs that will be running on desktop computers, as all programs are put into RAM anyway - you typically don't save anything by making such a declaration. It is however, considered to be good practice since it tells someone else that is reading your program that these are constants, and will never change.





The second reason is simply a promise that is made by a function not to change a variable. Consider the following prototype:





int memcmp (const void *s1, const void *s2, size_t n);





the first two parameters have "const" modifiers added. What this means is that it is a promise made by the "memcmp" function that it is not going to modify the data areas of either of the first two parameters.





This comes in handy in case you have worked hard to get data to be exactly as you want it to be, and you want to call another function that you didn't write. If you look at the prototype (as above), you can see that the function promises not to change your data, so it is safe to call it.





Here again, it is not an absolutely necessary thing to do, but it saves people time in having to check all of these functions to see if they muck up the parameters that are passed.


No comments:

Post a Comment