CS 70

Loops and Conditionals with Object Lifetime

Declaring Blocks

  • Duck speaking

    What's the difference is between “the closing curly brace of a function” and “the closing curly brace of a declaring block”?

  • LHS Cow speaking

    The closing curly brace of a function is always also the closing curly brace of a declaring block, but there are other places that we might find the closing curly brace of a declaring block, too. The closing curly brace of a for loop, and the closing curly brace of an if statement both end declaring blocks, but not functions.

  • Duck speaking

    Okay, but how would that change our memory models?

  • LHS Cow speaking

    We'll see in this example!

Imagine we had this program to trace:

                                       // Line
int main()                             //    1
{                                      //    2
    int x = 3;                         //    3
    for (int y = 0; y < 10; ++y) {     //    4
        if (y > x) {                   //    5
            cout << y << endl;         //    6
        }                              //    7
    }                                  //    8
    cout << x << endl;                 //    9
 }                                     //   10

The variable x is allocated in line 2.

x is initialized in line 3, where it's declared.

If we look up from line 3, the closest opening curly brace ({) is the one that starts the function, on line 2. Thus the block starting on line 2 is x's declaring block. x will be destroyed at the end of that block, on line 10. It will also be deallocated on line 10, since that is the end of the function.

The variable y is also allocated in line 2. y is initialized in line 4, where it is declared. The syntax of for loops is a little weird, but the curly brace at the end of line 4 is also the start of y's declaring block. y will be destroyed at the end of that block, on line 8. y will stay in that state—destroyed but not deallocated—until line 10, when we reach the closing curly brace of the function.

  • Duck speaking

    Wait… does that mean we couldn't print the value of y in line 9?

  • LHS Cow speaking

    Exactly! This is another place that C++ is different from a language like Python. In the equivalent Python program, we could totally print the value of y after we left the for loop. But in C++, the end of the declaring block ends the use phase of an object's lifetime. We're not allowed to refer to it at all once it's been destroyed.

You Try It!

Consider this C++ program:

                                       // Line
int main()                             //    1
{                                      //    2
    int x = 4;                         //    3
    int total = 0;                     //    4
    for (int z = 0; z < x; ++z) {      //    5
        total += z;                    //    6
    }                                  //    7
    int twice_total = 2 * total;       //    8
}                                      //    9

Which variables will be destroyed in line 7?

Which variables will be destroyed in line 9?

Continuing the previous example, if we wanted to add a new line between line 7 and line 8, which variables could be have their values printed?

  • Hedgehog speaking

    Phew!

(When logged in, completion status appears here.)