GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||
GeezerVison is showing en.wikipedia.org — Dynamic variable - Wikipedia
In computer programming, a dynamic variable is a local variable that is created to reside in manually allocated memory that can be allocated and deallocated. It is divided into three types, namely 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
let l = 10; // value stored in heap memory implicitly let b = 20; area = l * b;
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; }
int *a = new int(10); // allocating to heap dynamic variable a = new int(20); // re-allocating to same variable without freeing memory HistoryDuring the development of LISP language in the late 1950s, 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 used both heap and stack allocation for variable assignment. Pointer type and limited garbage collection for heap allocation was also used in it.[3] Designed in 1968–69, the dynamic variable was first named and defined in Pascal, a programming language developed by Niklaus Wirth in 1970. Here, the dynamic variables were described as pointers which stored the memory of referenced variables. Allocation of memory to the pointers was done by Dynamic object, pointers and the 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] AddressingIndirect addressing mode is used in these variables in which address is determined during runtime rather than assembly. This mode is found in x86 computer architecture. Scope and extentIn terms of scope (computer programming) and extent of variable, dynamic variable is local unlike static variable (global), which means 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 and to avoid memory leak, 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 more flexible than static objects by allowing dynamic memory allocation. In the below snippet, the constructor class 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
References
Further reading
Hidden categories:
| ||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |