Automatic variable

Article on other languages:

del.icio.us del.icio.us
Digg Digg
Furl Furl
Reddit Reddit
Rojo Rojo
Add to OnlyWire

In computer programming, an automatic variable is a lexically-scoped variable which is allocated and de-allocated automatically when program flow enters and leaves the variable's scope. The term local variable is usually synonymous with automatic variable, since these are the same thing in many programming languages.

Automatic variables may be allocated in the stack frame of the procedure in which they are declared; this has the useful effect of allowing recursion and re-entrancy. (For efficiency, the optimizer will try to allocate some of these variables in processor registers.)

In specific programming languages

C, C++

(Called local or automatic variables.)

All variables declared within a block of code are automatic by default, but this can be made explicit with the auto keyword.[1] An uninitialized automatic variable has an undefined value until it is assigned a valid value of its type.[2]

Using the storage class register instead of auto 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.

Java

(Called local variables.)

Similar to C and C++, but there is no auto or register keyword. A not-explicitly-initialized variable will have the value 0 (for numeric types), null (for reference types) or false (for the boolean type).[3]

Perl

(Called lexical, my or private variables.)

Declared using the my operator. Uninitialized scalars will have the value undef; uninitialized arrays or hashes will be ().[4]

See also

References and footnotes

  1. ^ Note: the future C++ standard, C++0x, may treat auto differently.
  2. ^ Current C standardPDF (3.61 MiB): section 6.2.4, Storage durations of objects
  3. ^ "4.12.5 Initial Values of Variables". Sun Microsystems. Retrieved on 2008-10-17.
  4. ^ "perlsub - perdoc.perl.org". Retrieved on 2008-10-17.


This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License.


Giant Panda

Mercedes Car
James Bond Guide
This site monitored by SitePinger.net