E.g
foo1() {
static const char* str = foo2();
}
const char * foo2 () {
...
}
How does the compiler makes sure it calls foo2 just once.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
foo2 is called at the initialisation of your program, just before main().Edit: this is wrong! I assumed this as this is how normally static initialisation works. But in this case, they are called once at the start of the function.
It must work with some kind of static boolean. Yep. At least in gcc, this:
Compiles to:
So it uses a hidden, function specific boolean (at $0x804a030) + some magic to protect against exceptions and multiple threads calling it at once.