Talk:Automatic variable
This article has not yet been rated on Wikipedia's content assessment scale. It is of interest to the following WikiProjects: | |||||||||||
|
Code sample
editThe 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); } }
Do automatic variables exist only in C?
editThis article is very C-specific - do "automatic" variables only exist in C?
129.67.18.164 (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?
editThe 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
orextern
. 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 likeauto
, 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 aregister
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.
- 129.67.18.164 (talk) 21:22, 8 February 2008 (UTC)
- PS: ...not forgetting dynamic allocation, of course (but this doesn't have a special keyword):
- Memory can be allocated/de-allocated with the
malloc
andfree
functions.
- Memory can be allocated/de-allocated with the
- 129.67.18.164 (talk) 21:28, 8 February 2008 (UTC)
Java Local Variable
editWhat 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 (talk • contribs) 01:17, 6 January 2009 (UTC)
Automatic and Stack Dynamic Variables
editI believe automatic and stack dynamic variables are the same but don't have a reference on hand. If so can we create a redirect page and put a brief sentence in the intro paragraph? Bhbuehler (talk) 07:28, 30 June 2018 (UTC)