Talk:Automatic variable

From Wikipedia, the free encyclopedia
Jump to: navigation, search
WikiProject Computing  
WikiProject icon This article is within the scope of WikiProject Computing, a collaborative effort to improve the coverage of computers, computing, and information technology on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
 ???  This article has not yet received a rating on the project's quality scale.
 ???  This article has not yet received a rating on the project's importance scale.

Code sample[edit]

The code example should be removed. It's unneeded, has undefined behaviour, and, as a matter of fact, does not work as described (on my machine) Dooooomi 14:53, 12 January 2007 (UTC)

It works for me iff I run gcc with no optimizations. I'll quick-fix the article's statement because I'm not sure what to do with the article overall (I probably agree with you; do whatever you think would improve the overall quality of the article, I guess...) —Isaac Dupree(talk) 21:20, 18 February 2007 (UTC)
oops again, I was wrong, it prints "1" for me then. Deleting... if it should be restored as a non-working example in a subsection when the article is more developed, that's okay —Isaac Dupree(talk) 21:23, 18 February 2007 (UTC)
For reference, the deleted code was:
 int main()
     int a;
     a = 10;
     int b;
     printf("b = %d\n", b);
Alksentrs (talk) 13:23, 18 October 2008 (UTC)

Do automatic variables exist only in C?[edit]

This article is very C-specific - do "automatic" variables only exist in C? (talk) 01:45, 31 January 2008 (UTC)

The AUTOMATIC keyword comes, at least, from PL/I before C existed. I believe automatic allocation came from ALGOL before that, but without the keyword. Gah4 (talk) 20:44, 10 June 2008 (UTC)

Storage class?[edit]

The C storage class for automatic variables is auto

What is meant with storage class? --Abdull (talk) 16:51, 6 February 2008 (UTC)

The storage class is specified using one of the keywords auto, register, static or extern. This tells the compiler how to store the variable:
  • auto (automatic - the default). These are allocated by the compiler when they come into scope, and de-allocated when they go out of scope.
  • register. Just like auto, but also provides a hint to the compiler to store the variable in a processor register (which is faster than main memory). Most modern compilers ignore this hint, since they are quite good at deciding for themselves where to put local variables. You can't take the address of a register variable.
  • static. The variable is allocated when it comes into scope, and sticks around until the program exits.
  • extern (external). The variable is defined elsewhere.
See also: C syntax#Storage duration specifiers. (talk) 21:22, 8 February 2008 (UTC)
PS: ...not forgetting dynamic allocation, of course (but this doesn't have a special keyword): (talk) 21:28, 8 February 2008 (UTC)

Java Local Variable[edit]

What the article says is completely wrong. To quote the current revision of the page:

A not-explicitly-initialized variable will have the value 0 (for numeric types), null (for reference types) or false (for the boolean type).[3]

Okay, nice that a reference ([3]) is provided, but when I go there, this is what I read there:

A local variable (§14.4, §14.14) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26), in a way that can be verified by the compiler using the rules for definite assignment (§16).

And as a Java programmer, I can confirm that the compiler will throw an error for each local variable not getting initialized before being used. What is currently written in the article is true for Instance Variables of Objects (this dynamically allocated memory is zero'ed on allocation), but we are talking about Local (Automatic) Variables here, aren't we? And I guess it can't be said more clearly than the quote above (which actually starts with "A local variable..."). Even in Java these are just stack variables and just like in C, they have no implicit value (actually they have a value, being whatever is currently found at the stack position - but the compiler won't allow you to use this value, it is not deterministic). —Preceding unsigned comment added by Mecki78 (talkcontribs) 01:17, 6 January 2009 (UTC)