Itachi Uchiwa
Itachi Uchiwa

Reputation: 3164

C++ primer 5 Ed Global objects

In C++ primer 5 Ed Chapter 12 The: Dynamic memory. It is said: "static objects are allocated before they are used, and they are destroyed when the program ends."

Does this mean the global objects are defined and initialized before control passes by their declaration.

I have this example:

constexpr double PI = 3.14;

double Perim(double rad) {
    std::cin.get(); // I want to block here waiting for user interaction
    return rad * 2 * PI;
}

constexpr double radius = 4.16;
double perim = Perim(radius); // Perim will blcok until user presses a key
int y;

So above is y created and initialized before user presses a key or it waits until control passes by its definition?

Upvotes: 0

Views: 45

Answers (1)

Paul Evans
Paul Evans

Reputation: 27577

If your posted code is in the global namespace then y will be allocated and set to 0 before main is run. It can only be used, however, after the line it is declared and defined on.

Upvotes: 2

Related Questions