Share on Facebook Share on Twitter Email
Answers.com

Automatic variable

 
Wikipedia: Automatic variable

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. However, the Java compiler will not allow the usage of a not-explicitly-initialized local variable and 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.[3] This differs from instance variables, which are implicitly initialized with default values.

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. http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12.5. Retrieved 2008-10-17. 
  4. ^ "perlsub - perdoc.perl.org". http://perldoc.perl.org/perlsub.html#Private-Variables-via-my(). Retrieved 2008-10-17. 



Search unanswered questions...
Enter a question here...
Search: All sources Community Q&A Reference topics
 
 

 

Copyrights:

Wikipedia. This article is licensed under the Creative Commons Attribution/Share-Alike License. It uses material from the Wikipedia article "Automatic variable" Read more