In general, a name must be declared or defined before it is used. In most other languages, there is no difference between a “declaration” and a “definition”. But, in C/C++, “definition” is to reserve storage for a name (variable, function,....); whereas “declaration” indicates to the compiler what a name represents. A name may be defined in a function or a module and used in another function or module. The C++ compiler works with only one source module at a time and therefore requires all names to be declared to eliminate the “undeclared name” error. The reserved work “extern” is used to tell the compile that a name is defined externally to the current function or file. The linker, on the other hand, must check to make sure each declared name is defined.
Variables C++ language can generally be categorized into the
following 6 groups:
• Local variables (Temporary
life time):
Variables defined
in the header of a function or inside a function body.
• External variables
(Permanent
life time):
Variables defined
outside any functions.
• Static variables
(Permanent
life time)
• Variables created
by dynamic
memory allocation functions (Permanent life time)
• Instance variables
(attributes)
(have the same life time as the object)
• Static attributes
(also
known as class attributes/variables) (Permanent life time)
Memory Map for a C/C++ Program
| Stack | Local variables, Return values, Return addresses |
| Heap | Free memory for dynamic allocation |
| Data Region | External variables, Static variables, String constants |
| Text Region | Program instructions |
General concepts
about Variables (language independent)
A variable denotes a storage capacity. It is a unit
(series) of bytes and can be considered as an abstraction of the
computer memory. The size of a variable is determined either by
the CPU or by the language compiler. A variable is a symbol used
to represent a unit of memory; whereas all memory cells (bytes) are
numbered by the operating system and treated as absolute memory.
Unlike in the assembly languages, it is usually not possible to
reference to the absolute address in the high level programming
languages. A variable is an entity or object which is composed of
the following attributes/properties:
• Name
The name of a variable is used to represent both the rvalue (RHS, the
data/value/contents)and lvalue (LHS, the storage/address). Both
rvalue and lvalue are fixed constant at a given point of time.
The address associated with a variable is determined by the operating
system, whereas the contents stored in variable is determined by the
programmer and the program logic. A variable usually needs to be
declared before it is used. A declaration can be implicit or
explicit. In an implicit declaration, the name of a variable may
suggest/indicate its data type. In an interpreted language, the
first time a variable is used is the time it is declared; the type of
the value assigned to the variable determines the variable type.
In some languages, “declaration” is not the same as “definition”
because “definition” may mean the reservation of the memory storage for
a variable; whereas “declaration” simply indicates the intention of
using a name that was previously defined.
• Type
The type of a variable denotes the domain of the values that can be
stored/assigned to a variable. It also determines a set of
pre-defined (by the language compiler) operations that can be applied
its contents. In object oriented programming languages, a type is
a subset of a class. A type is a template used to create
variables, whereas a class is a template used to create objects.
• Alias
It is possible that a variable may be referred to by many names, each
of such names is termed as an alias or a reference. An alias is
NOT the same as a pointer.
• Size
The size of the storage associated with a variable is usually
determined by the CPU on which the program executes. However, to
ensure cross-platform portability, the language compile may define a
fixed size for each type.
• Initialization
Assigning a value to a variable at its creation time is termed as
“initialization” and happens only once for the life time of a
variable. Such an operation may require a special syntax,
particularly when initializing a complex variable. A variable may
also be used a constant variable which must be initialized and cannot
change its contents thereafter. The time the storage is allocated
for a variable is the initialization time.
• Value
The value of a variable is also known as the rvalue, the data, or the
contents. Usually, a variable is able to store one single value
at any given point of time, except a set variable which represents a
set of values.
• Address
The address associated with the storage of a variable and is sometimes
termed as lvalue. It is assigned by the operating system at the
binding time. The binding time may vary depending on the storage
type of the variable. The binding can either be static binding
which happens before the program execution or dynamic binding which
takes place during the program execution.
• Scope
The scope of a variable determined its accessibility/visibility and is
usually divided into two levels: Global (external) and Local
(internal). A local variable may be local to a sub-program or a
block which defines it. The scope of a variable usually also
determines its life time. Some programming languages provide the
feature to allow the life time a variable to be changed – that is, to
change the life time of a local variable from temporary to
permanent. Changing the life time of a variable does not change
its scope.
• Life Time (Storage Type)
The life time of a variable is usually determined by its scope.
For example, a local variable has the same life time as the sub-program
which defines it. In other words, when the sub-program finishes
its execution, all its variables will be destroyed automatically by the
operating system. The global (external) variable has the same
life time as the program. Hence, there are two major categories
of storage: Static and Dynamic. Further, there are two kinds of
dynamic storage: Heap and Stack. The stack is automatically
managed by the operating system and is used for local variables,
returned values, arguments passed to sub-programs. On the other
hand, the heap space can be used by a program during the program
execution time and is usually the programmer’s responsibility to
release such a memory storage.