Jump to content

Automatic variable

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Ira Leviton (talk | contribs) at 20:49, 9 October 2019 (Replaced html coding found with Wikipedia:Typo Team/moss.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer programming, 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,[note 1] 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.[note 2] Automatic local variables are normally allocated in the stack frame of the procedure in which they are declared.[note 3] This was originally done to achieve re-entrancy and allowing recursion,[note 4] 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 in the late 1950s, and further popularized by its many descendants.

The term local variable 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 variables 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 of code are automatic by default. An uninitialized automatic variable has an undefined value until it is assigned a valid value of its type.[1]

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 referencing 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. See Resource Acquisition Is Initialization (RAII). Note, C++11 has a new auto specifier[1], which is different. In this case, the variable's type is inferred.

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.[2] This differs from instance variables, which are implicitly initialized with default values (which are 0 for numbers and null 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 ().[3]

Perl also has a local operator that does not create automatic variables,[4] instead giving global (package) variables a temporary value, which is dynamically scoped to the enclosing block. When the scope of the variable is left, the old value is restored.

See also

Notes

  1. ^ unless it is a nested function, which itself is defined along that local data
  2. ^ although they exist in a somewhat similar, but not identical, form also in recursive languages with dynamic scoping, such as older variants of LISP
  3. ^ unless otherwise specified, such as static or heap-based data, which are specifiable in some languages
  4. ^ When the reentrant property of the routine is used, for recursion or otherwise, the optimizer must not try to allocate such variables in processor registers (for efficiency) as this would break the reentrancy.

References

  1. ^ Current "C standard" (PDF). (3.61 MiB): section 6.2.4, Storage durations of objects
  2. ^ "4.12.5 Initial Values of Variables". Sun Microsystems. Retrieved 2008-10-17.
  3. ^ "Private variables via my() - perlsub - perldoc.perl.org". Retrieved 2008-10-17.
  4. ^ "Temporary values via local() - perlsub - perldoc.perl.org". Retrieved 2011-02-25.