Dynamic variable
In computer programming, a dynamic variable is a local variable that is created to have a manually allocated memory which can be allocated and deallocated. It is divided into four types namely stack dynamic variable, implicit heap dynamic variable, explicit heap dynamic variable and lost heap dynamic variable (dangling pointers). It has indefinite scope and dynamic extent. It is mostly used in niche programming languages which include Lisp, Ruby, APL, PHP, TcL, TEX and PERL.[1]

Types of dynamic variables
[edit]- Stack dynamic variable – local variables which is bound when the declaration statement is executed and deallocated when the procedure returns. e.g. C subprograms and Java methods. In the below implementation of nested subprograms without using recursion, the activation record builds from bottom to top in the stack using dynamic memory allocation.
void fun1(float r) { int s, t[3]; fun2(s); // fun1 calls fun2 } void fun2(int x) { int y; fun3(y); // fun2 calls fun3 } void fun3(int q) { } void main() { float p; fun1(p); // main calls fun1 }
- Implicit heap dynamic variable – dynamic variables where the values are bound to heap storage on assignment and allocation with release occurs when new value is assigned e.g. all variables in JavaScript, PHP and APL. More flexible than explicit heap dynamic variable as larger memory can be allocated when the required memory size is unknown.
let l = 10; // value stored in heap memory implicitly
let b = 20;
area = l * b;
- Explicit heap dynamic variable – dynamic variables that are allocated and deallocated by explicit run–time instructions as specified by the programmer. Examples include dynamic objects in C++ and objects in Java.
int main() {
int* y = new int(1); // creating dynamic variable *y
for (int i = 0; i < 5; i++) {
if (*y <= 5)
cout << *y << endl;
(*y)++; // increment the value via pointer
}
delete y; // deleting variable to prevent memory leak
return 0;
}
- Lost heap dynamic variable – dynamic variables no longer referenced by the pointer. When the heap dynamic variable is lost, the process is called memory leak. It is created by a first pointer pointing to new heap dynamic variable and then the pointer is shifted to another heap dynamic variable. The first heap dynamic variable if not used will be lost.
int *a = new int(10); // allocating to heap dynamic variable
a = new int(20); // re-allocating to same variable without freeing memory
History
[edit]During the development of LISP language in late 50s, garbage memory collection as a form of dynamic memory management was used in automatic and dynamic allocation in LISP. It was meant to automate memory management instead of the programmer to manually delete used memory space. Algorithms such as mark and sweep, reference counting and copy algorithm were fundamental to garbage collection concept. Although John McCarthy developed these algorithms, both McCarthy and Marvin Minsky were key figures to develop LISP.[2]
Earlier concepts of dynamic variable was used in ALGOL 68 which had both heap and stack allocation for variable assignment. Pointer type and limited garbage collection for heap allocation was used in it also.[3]
Designed in 1968–69, the dynamic variables was first named and defined in Pascal, a programming language developed by Niklaus Wirth in 1970, with the variables to be described as pointers which stored the memory of referenced variables. Allocation of memory to the pointers was done by NEW and deallocation by DISPOSE command.[4]
Dynamic object, pointers and different types of dynamic variables across different programming languages was first described in depth in the 1989 book by Robert Sebesta, "Concepts of Programming Languages".[5]
Addressing
[edit]Indirect addressing mode is used in these variables in which address is determined during runtime rather than assembly. For stack based dynamic variable, stack addressing mode is used which uses stack pointer (SP) and frame pointer (FP). During push operation, SP moves up and during pop operation, SP moves down; and the FP serves as an offset reference from which SP is calculated. Both the addressing modes are used in x86 computer architecture.
Scope and extent
[edit]In terms of scope (computer programming) and extent of variable, dynamic variable is local unlike static variable (global), which means that it can be only used in the function where it is defined and to reuse it with a different value will require deallocation and re-allocation. The lifetime of dynamic variable is controlled by the programmer. Memory to the dynamic variable is allocated on run-time. To avoid memory leaks, the dynamic variable must be deallocated before re-use.
Simply put, dynamic variables have indefinite scope, meaning the variable binding can be seen by any code used in the program and dynamic extent, meaning variable binding is seen only between the time of creation and time when control leaves the code using dynamic variable during execution. Hence it can't be seen in Python as all created variables are global or having lexical scope.
Dynamic objects are created in main method when the number of objects to be used is unknown thus making it flexible more than static objects by allowing dynamic memory allocation. In the below snippet, the constructor class Counter is automatically invoked when dynamic object *c is created and the value 5 is assigned. When the control delete the object, the destructor ~Counter is called and the output is printed.
class Counter {
int value;
public:
Counter(int start) { // Constructor called on object initialization
value = start;
}
~Counter() { // Destructor called when object destroyed
value--;
cout << value; // prints 4
}
};
int main() {
Counter* c = new Counter(5);
delete c;
return 0;
}
See also
[edit]- Variable (high-level programming language) – Named container for a particular type of data
- Stack-based memory allocation – Form of computer memory allocation
- C dynamic memory allocation – Dynamic memory management in the C programming language
References
[edit]- ^ "Dynamic variables". dl.acm.org. doi:10.1145/378795.378857. Retrieved 2026-05-29.
- ^ Nystrom, Robert (2021-07-27). Crafting Interpreters. Genever Benning. ISBN 978-0-9905829-4-6.
- ^ Branquart, Paul, and Johan Lewi. "A Scheme of Storage Allocation and Garbage Collection for Algol 68." Algol 68 Implementation. 1970.
- ^ Price, Ana M. de A. (February 1984). "Defining dynamic variables and abstract data types in Pascal". ACM SIGPLAN Notices. 19 (2): 85–91. doi:10.1145/948566.948574. ISSN 0362-1340.
- ^ Sebesta, R. W. (2016). Concepts of programming languages. Pearson Education India.
Further reading
[edit]- Dynamic Variables David R. Hanson and Todd A. Proebsting. November 2000.