Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When static global variable are created in C++

I know that global variables are created in declaration order for same compilation unit and creation order is not defined between multiple compilation units.

I've read some time ago that global variables are created before any code from the compilation unit where they are defined is called. Is this defined by the standard?

Example:

file1.cpp

int f1v1 = f1_1();
int f1v2 = f1_2();

void f1(){...}

int f1_1(){...}
int f1_2(){...}

file2.cpp

static int f2v1 = f2_1();
static int f2v2 = f2_2();

int f2_1(){...}
int f2_2(){...}

main.cpp

#include "file1.h"
#include "file2.h"

int main()
{
    f1();

    return 0;
}

Is in this case guaranteed by the standard that f1_1() is called before f1_2() and before f1()? Is guaranteed by the standard that f2_1() and f2_2() are called at all as no function defined in file2.cpp is called and f2v1 and f2v2 are not visible outside file2.cpp?

EDIT:

Is the behavior specified by the standard when file1.cpp is compiled in lib1 and file2.cpp is compiled in lib2?

like image 714
Mircea Ispas Avatar asked Dec 04 '12 10:12

Mircea Ispas


People also ask

Can global variables be static in C?

So if you save the program as a C++ program, it would compile and run fine. 5) Static global variables and functions are also possible in C/C++. The purpose of these is to limit scope of a variable or function to a file.

Is global variable static by default?

Global and static variables are initialized to their default values because it is in the C or C++ standards and it is free to assign a value by zero at compile time. Both static and global variable behave same to the generated object code.

Should a global variable be static?

If global variable is to be visible within only one . c file, you should declare it static. If global variable is to be used across multiple . c files, you should not declare it static.

Where are global static variables stored in C?

The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program. All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment).


1 Answers

Based on [basic.start.init]:

f1_1() is guaranteed to execute before f1_2() (para.2: "Variables with ordered initialization defined within a single translation unit shall be initialized in the order of their definitions in the translation unit.").

Both are guaranteed to execute before f1() (para.4: "If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first odr-use (3.2) of any function or variable defined in the same translation unit as the variable to be initialized.").

About f2_1() and f2_2(), [basic.stc.static]para.2 says "If a variable with static storage duration has initialization or a destructor with side effects, it shall not be eliminated even if it appears to be unused, ...". This would imply they are guaranteed to be called if they contain side effects.

like image 102
Angew is no longer proud of SO Avatar answered Oct 20 '22 00:10

Angew is no longer proud of SO