Constant Variables : understanding the concept

Share the Article

Built-in types & complex variables

The variables in C++ are of two different types. Firstly, they are simple built-in type, like, int, or float or char etc. And secondly, they are complex data types, like, objects, arrays etc. The C++ compiler has different ways to handle them (normal variables and constant variables)

Built-in & complex constants

However, if these variables are “const”, then there are few interesting things to see. For example, in case of built-in const, the compiler may keep the constant value in symbol table. However, when such variables are of complex types, then compiler has to store the const values in appropriate memory location which may not be possible in compile time. Therefore, unlike built-in, the const values of complex types are not be available at compile time.

The following example displays how the compiler treats the constant values differently.

//Built-in types const int i[] = { 1, 2, 3, 4 }; // array having const int float f[i[3]]; // Illegal to use during compilation //complex types struct node { int i; int j; }; const node s[] = { { 1, 2 }, { 3, 4 } }; // constant object double d[s[1].j]; // Illegal to use values from object

Forcing the compiler to allocate storage space for built-in types

As explained above, in general case, the C++ compiler do not create additional storage for a constants of built-in type. Instead the compiler holds the definition value in symbol table.

However, when program uses “extern” keyword with const, then it forces compiler to allocate storage. This is also true for cases, whenever, program takes the address of a const)

extern const int MAXSIZE;

External Linkage:

The extern means to use external linkage. This means that code from other compilation units are allowed to use this variable. Therefore, compiler has to make sure that program gets run-time storage for any such variables. Basically, due to run-time storage, even constant data value will not be available at compile time.

const int ib = 8; int x[ib]; //This is ok extern const int ia = 8; int x[ia]; //This is NOT ok

compiler will generate error:

compiler error due to extern keyword with constant variable

Main Funda: The way compiler treats constant in Built-in and Complex types is very different

Related Topics:

What are differences : #define & const built-in types?
Understanding Constant Variables
Why variable declaration should not be done at the start? 
Concept of Inline Functions
Understanding array version of new[] & delete[]
How to overload global version of new & delete ?
How to overload new & delete for a class?
Explaining C++ casts
How to overload placement new operator?
Benefit of using “override” keyword?
What are Lambda?
How to stop compiler from generating special member functions?
 What are the drawbacks of using enum ?
Parametrized constructor
What is an explicit constructor ?
Basics of throwing and catching exception

Share the Article

Leave a Reply

Your email address will not be published. Required fields are marked *