HOME

TheInfoList



OR:

In
computer programming Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, an automatic variable is a local variable which is allocated and deallocated automatically when program flow enters and leaves the variable's scope. The scope is the lexical context, particularly the function or block in which a variable is defined. Local data is typically (in most languages) invisible outside the function or lexical context where it is defined. Local data is also invisible and inaccessible to a ''called'' function,unless it is a
nested function In computer programming, a nested function (or nested procedure or subroutine) is a named function that is defined within another, enclosing, block and is lexically scoped within the enclosing block meaning it is only callable by name within t ...
, which itself is ''defined'' along that local data
but is not deallocated, coming back in scope as the execution thread returns to the caller. Automatic local variables primarily applies to recursive lexically-scoped languages.although they exist in a somewhat similar, but not identical, form also in recursive languages with
dynamic scoping In computer programming, the scope of a name binding (an association of a name to an entity, such as a variable) is the part of a program where the name binding is valid; that is, where the name can be used to refer to the entity. In other parts ...
, such as older variants of
LISP Lisp (historically LISP, an abbreviation of "list processing") is a family of programming languages with a long history and a distinctive, fully parenthesized Polish notation#Explanation, prefix notation. Originally specified in the late 1950s, ...
Automatic local variables are normally allocated in the stack frame of the procedure in which they are declared.unless otherwise specified, such as static or heap-based data, which are specifiable in some languages This was originally done to achieve re-entrancy and allowing
recursion Recursion occurs when the definition of a concept or process depends on a simpler or previous version of itself. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in m ...
,When the reentrant property of the routine is used, for recursion or otherwise, the optimizer must ''not'' try to allocate such variables in
processor register A processor register is a quickly accessible location available to a computer's processor. Registers usually consist of a small amount of fast storage, although some registers have specific hardware functions, and may be read-only or write-onl ...
s (for efficiency) as this would break the reentrancy.
a consideration that still applies today. The concept of automatic variables in recursive (and nested) functions in a lexically scoped language was introduced to the wider audience with
ALGOL ALGOL (; short for "Algorithmic Language") is a family of imperative computer programming languages originally developed in 1958. ALGOL heavily influenced many other languages and was the standard method for algorithm description used by the ...
in the late 1950s, and further popularized by its many descendants. The term ''
local variable In computer science, a local variable is a variable that is given ''local scope''. A local variable reference in the function or block in which it is declared overrides the same variable name in the larger scope. In programming languages with ...
'' is usually synonymous with automatic variable, since these are the same thing in many programming languages, but local is more general – most local variables are automatic local variables, but
static local variable In computer science, a local variable is a Variable (programming), variable that is given ''local scope (programming), scope''. A local variable reference in the subroutine, function or block (programming), block in which it is declared overrides ...
s also exist, notably in C. For a static local variable, the allocation is static (the lifetime is the entire program execution), not automatic, but it is only in scope during the execution of the function.


In specific programming languages


C, C++

(Called ''automatic'' variables.) All variables declared within a
block Block or blocked may refer to: Arts, entertainment and media Broadcasting * Block programming, the result of a programming strategy in broadcasting * W242BX, a radio station licensed to Greenville, South Carolina, United States known as ''96.3 ...
of code are automatic by default. An uninitialized automatic variable has an
undefined Undefined may refer to: Mathematics *Undefined (mathematics), with several related meanings **Indeterminate form, in calculus Computing *Undefined behavior, computer code whose behavior is not specified under certain conditions *Undefined valu ...
value until it is assigned a valid value of its type. The storage-class specifier auto can be added to these variable declarations as well, but as they are all automatic by default, this is entirely redundant and rarely done. In C, using the storage class register is a hint to the compiler to cache the variable in a processor register. Other than not allowing the address-of operator (&) to be used on the variable or any of its subcomponents, the compiler is free to ignore the hint. In C++, the constructor of automatic variables is called when the execution reaches the place of declaration. The destructor is called when it reaches the end of the given program block (program blocks are surrounded by curly brackets). This feature is often used to manage resource allocation and deallocation, like opening and then automatically closing files or freeing up memory, called
Resource Acquisition Is Initialization Resource acquisition is initialization (RAII) is a programming idiom used in several object-oriented, statically typed programming languages to describe a particular language behavior. In RAII, holding a resource is a class invariant, and is tie ...
(RAII). Since C++11, C++ allows variables to be declared with the auto type specifier, but this means that the variable's type is
inferred Inferences are steps in logical reasoning, moving from premises to logical consequences; etymologically, the word ''infer'' means to "carry forward". Inference is theoretically traditionally divided into deduction and induction, a distinction ...
, and does not refer to the scope of the variable.


Java

(Called ''local variables''.) Similar to C and C++, but there is no auto or register keyword. However, the Java compiler will not allow the usage of a not-explicitly-initialized local variable and will give a compilation error (unlike C and C++ where the compiler will usually only give a warning). The Java standard demands that every local variable must be explicitly initialized before being used. This differs from instance variables, which are implicitly initialized with default values (which are for numbers and for objects).


Perl

(Called ''lexical'', ''my'' or ''private'' variables.) In Perl, local variables are declared using the my operator. Uninitialized scalars will have the value undef; uninitialized arrays or hashes will be (). Perl also has a
local Local may refer to: Geography and transportation * Local (train), a train serving local traffic demand * Local, Missouri, a community in the United States Arts, entertainment, and media * ''Local'' (comics), a limited series comic book by Bria ...
operator that does not create automatic variables, instead giving global (package) variables a temporary value, which is
dynamically scoped In computer programming, the scope of a name binding (an association of a name to an entity, such as a Variable (programming), variable) is the part of a Computer program, program where the name binding is valid; that is, where the name can be use ...
to the enclosing block. When the scope of the variable is left, the old value is restored.


See also

* C syntax#Storage class specifiers *
Variable-length array In computer programming, a variable-length array (VLA), also called variable-sized or runtime-sized, is an array data structure whose length is determined at runtime, instead of at compile time. In the language C, the VLA is said to have a variab ...
( C99 new feature) *
Call stack In computer science, a call stack is a Stack (abstract data type), stack data structure that stores information about the active subroutines and block (programming), inline blocks of a computer program. This type of stack is also known as an exe ...
*
Object lifetime Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ...


Notes


References

{{Memory management Memory management Variable (computer science) es:Asignación automática de memoria de:Automatische Variable