In my mind, always, definition means storage allocation.
In the following code, int i
allocates a 4-byte (typically) storage on program stack and bind it to i
, and i = 3
assigns 3 to that storage. But because of goto
, definition is bypassed which means there is no storage allocated for i
.
I heard that local variables are allocated either at the entry of the function (f()
in this case) where they reside, or at the point of definition.
But either way, how can i
be used while it hasn't been defined yet (no storage at all)? Where does the value three assigned to when executing i = 3
?
void f()
{
goto label;
int i;
label:
i = 3;
cout << i << endl; //prints 3 successfully
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…