Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 774785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:11:16+00:00 2026-05-14T19:11:16+00:00

E.g foo1() { static const char* str = foo2(); } const char * foo2

  • 0

E.g

 foo1() {
     static const char* str = foo2();

 }

 const char * foo2 () {

     ...
 }

How does the compiler makes sure it calls foo2 just once.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:11:16+00:00Added an answer on May 14, 2026 at 7:11 pm

    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:

    int test2()
    {
        static int bla = test();
    }
    

    Compiles to:

     8048616: b8 30 a0 04 08        mov    $0x804a030,%eax
     804861b: 0f b6 00              movzbl (%eax),%eax
     804861e: 84 c0                 test   %al,%al
     8048620: 75 52                 jne    8048674 <_Z5test2v+0x67>
     ...
     804863c: e8 b3 ff ff ff        call   80485f4 <_Z4testv>
     ...
     8048674: 83 c4 1c              add    $0x1c,%esp
     8048677: 5b                    pop    %ebx
     8048678: 5e                    pop    %esi
     8048679: 5f                    pop    %edi
     804867a: 5d                    pop    %ebp
     804867b: c3                    ret    
    

    So it uses a hidden, function specific boolean (at $0x804a030) + some magic to protect against exceptions and multiple threads calling it at once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example: void main(){ foo1(); } fooTarget(); foo1(foo2();); foo2(foo3();); foo3(foo4();); foo4(foo5();); foo5( foo6(); fooTarget();
I have a file that contains: foo1 = 1 foo2 = 2 foo3 =
> a = %w[foo1 foo2 foo3 foo4 foo5 foo6] => [foo1, foo2, foo3, foo4,
I have a set of column names in a table - e.g. foo1, foo2,
var foo1,foo2; switch (fn) { case fade : foo1 = fadeOut; foo2 = fadeIn;
I have two implementations of interface IFoo : Foo1 and Foo2 . I need
class Foo { } class Foo1 : Foo { } class Foo2 : Foo
I have a string: 123 + FOO1[ccc + e_FOO1 + ddd + FOO2[b_FOO2]] =
I just want to properly translate this foo.push((000 + parseInt(foo1.charAt(loc3), 16).toString(2)).slice(-4)); into php. Anyone
I would like query like SELECT 'FOO1', 'FOO2' But returns the resultset into rows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.