Variables in Programming Languages: A Comprehensive Guide

Von Neumann Bottleneck

The speed of connection between memory and the processor is slower than the speed at which instructions can be executed.

Compiler Implementation

A program is translated into machine language, which can be directly executed (e.g., C, COBOL, C++, ADA).

Interpretation

No translation is performed; the program is interpreted by another program (e.g., JavaScript, PHP).

Hybrid Implementation

High-level language programs are interpreted into an intermediate language that can be easily interpreted.

Variables in Programming

What is a Name?

A name is a string of characters used to identify an entity in a program.

What is a Variable?

A variable is an abstraction of a computer memory cell or a collection of cells.

Six Attributes of a Variable:

  • Name
  • Address
  • Value
  • Type
  • Lifetime
  • Scope

Storage Binding

Allocation is the process of taking a memory cell from the memory pool and binding it to a variable. Deallocation is the process of returning a memory cell to the available pool after it is unbound from a variable.

Variable Lifetime

The lifetime of a variable is the time during which the variable is bound to a specific memory location. It begins when the variable is bound to a cell and ends when it is unbound.

Types of Variables Based on Lifetime

Static Variables

Static variables are bound to memory cells before program execution begins and remain bound to those same cells until program execution terminates.

Advantages of Static Variables:
  • Efficiency: Direct addressing is possible, leading to faster access.
  • No Runtime Overhead: Allocation and deallocation are done at compile time.
Disadvantages of Static Variables:
  • Reduced Flexibility: Cannot support recursive subprograms.
  • No Storage Sharing: Storage cannot be shared among variables.

Stack-Dynamic Variables

Stack-dynamic variables have their storage bindings created when their declaration statements are elaborated, but their types are statically bound. Elaboration refers to the runtime process of storage allocation and binding indicated by the declaration.

Advantages of Stack-Dynamic Variables:
  • Support for Recursion: Allow each active copy of a recursive subprogram to have its own version of local variables.
Disadvantages of Stack-Dynamic Variables:
  • Runtime Overhead: Allocation and deallocation occur at runtime.
  • Slower Access: Indirect addressing may be required.
  • No History Sensitivity: Subprograms cannot retain values between calls.

Explicit Heap-Dynamic Variables

Explicit heap-dynamic variables are nameless (abstract) memory cells allocated and deallocated by explicit runtime instructions written by the programmer. They are accessed through pointer or reference variables. The heap is a disorganized collection of storage cells.

In C++, the new operator is used to create explicit heap-dynamic variables. It takes a type name as an operand and returns the address of the newly created variable.

Advantages of Explicit Heap-Dynamic Variables:
  • Dynamic Data Structures: Useful for creating dynamic structures like linked lists and trees.
Disadvantages of Explicit Heap-Dynamic Variables:
  • Complexity: Difficult to use pointers and references correctly.
  • Cost of References: Accessing variables through pointers or references adds overhead.
  • Storage Management: Implementing storage management for the heap is complex.

Implicit Heap-Dynamic Variables

Implicit heap-dynamic variables are bound to heap storage only when they are assigned values. All their attributes are bound at assignment time.

Advantages of Implicit Heap-Dynamic Variables:
  • Flexibility: Allow for highly generic code.
Disadvantages of Implicit Heap-Dynamic Variables:
  • Runtime Overhead: Maintaining dynamic attributes adds overhead.
  • Reduced Error Detection: Compilers may not be able to detect some errors.