Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a variable be used when its definition is bypassed?

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
}
like image 821
Eric Z Avatar asked Dec 16 '11 14:12

Eric Z


2 Answers

Long story short; goto will result is a runtime jump, variable definition/declaration will result in storage allocation, compile time.

The compiler will see and decide on how much storage to allocate for an int, it will also make so that this allocated storage will be set to 3 when "hitting" i = 3;.

That memory location will be there even if there is a goto at the start of your function, before the declaration/definition, just as in your example.


Very silly simile

If I place a log on the ground and my friend runs (with his eyes closed) and jumps over it, the log will still be there - even if he hasn't seen or felt it.

It's realistic to say that he could turn around (at a later time) and set it on fire, if he wanted to. His jump doesn't make the log magically disappear.

like image 79
Filip Roséen - refp Avatar answered Oct 20 '22 08:10

Filip Roséen - refp


Your code is fine. The variable lives wherever it would live had the goto not been there.

Note that there are situations where you can't jump over a declaration:

C++11 6.7 Declaration statement [stmt.dcl]

3 It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps from a point where a variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has scalar type, class type with a trivial default constructor and a trivial destructor, a cv-qualified version of one of these types, or an array of one of the preceding types and is declared without an initializer (8.5). [ Example:

void f()
{
    // ...
    goto lx;    // ill-formed: jump into scope of `a'
    // ...
ly:
    X a = 1;
    // ...
lx:
    goto ly;    // ok, jump implies destructor
                // call for `a' followed by construction
                // again immediately following label ly
}

—end example ]

like image 32
NPE Avatar answered Oct 20 '22 08:10

NPE