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

  • Home
  • SEARCH
  • 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 6669255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:07:42+00:00 2026-05-26T03:07:42+00:00

In the following code: int count(){ static int n(5); n = n + 1;

  • 0

In the following code:

int count(){
    static int n(5);
    n = n + 1;
    return n;
}

the variable n is instantiated only once at the first call to the function.

There should be a flag or something so it initialize the variable only once.. I tried to look on the generated assembly code from gcc, but didn’t have any clue.

How does the compiler handle this?

  • 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-26T03:07:43+00:00Added an answer on May 26, 2026 at 3:07 am

    This is, of course, compiler-specific.

    The reason you didn’t see any checks in the generated assembly is that, since n is an int variable, g++ simply treats it as a global variable pre-initialized to 5.

    Let’s see what happens if we do the same with a std::string:

    #include <string>
    
    void count() {
        static std::string str;
        str += ' ';
    }
    

    The generated assembly goes like this:

    _Z5countv:
    .LFB544:
            .cfi_startproc
            .cfi_personality 0x3,__gxx_personality_v0
            .cfi_lsda 0x3,.LLSDA544
            pushq   %rbp
            .cfi_def_cfa_offset 16
            movq    %rsp, %rbp
            .cfi_offset 6, -16
            .cfi_def_cfa_register 6
            pushq   %r13
            pushq   %r12
            pushq   %rbx
            subq    $8, %rsp
            movl    $_ZGVZ5countvE3str, %eax
            movzbl  (%rax), %eax
            testb   %al, %al
            jne     .L2                     ; <======= bypass initialization
            .cfi_offset 3, -40
            .cfi_offset 12, -32
            .cfi_offset 13, -24
            movl    $_ZGVZ5countvE3str, %edi
            call    __cxa_guard_acquire     ; acquire the lock
            testl   %eax, %eax
            setne   %al
            testb   %al, %al
            je      .L2                     ; check again
            movl    $0, %ebx
            movl    $_ZZ5countvE3str, %edi
    .LEHB0:
            call    _ZNSsC1Ev               ; call the constructor
    .LEHE0:
            movl    $_ZGVZ5countvE3str, %edi
            call    __cxa_guard_release     ; release the lock
            movl    $_ZNSsD1Ev, %eax
            movl    $__dso_handle, %edx
            movl    $_ZZ5countvE3str, %esi
            movq    %rax, %rdi
            call    __cxa_atexit            ; schedule the destructor to be called at exit
            jmp     .L2
    .L7:
    .L3:
            movl    %edx, %r12d
            movq    %rax, %r13
            testb   %bl, %bl
            jne     .L5
    .L4:
            movl    $_ZGVZ5countvE3str, %edi
            call    __cxa_guard_abort
    .L5:
            movq    %r13, %rax
            movslq  %r12d,%rdx
            movq    %rax, %rdi
    .LEHB1:
            call    _Unwind_Resume
    .L2:
            movl    $32, %esi
            movl    $_ZZ5countvE3str, %edi
            call    _ZNSspLEc
    .LEHE1:
            addq    $8, %rsp
            popq    %rbx
            popq    %r12
            popq    %r13
            leave
            ret
            .cfi_endproc
    

    The line I’ve marked with the bypass initialization comment is the conditional jump instruction that skips the construction if the variable already points to a valid object.

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

Sidebar

Related Questions

I have the following code: public class LSD{ public static int R=1<<8; public static
I currently have the following code: public class Count { public static void countChar()
I saw the following code: http://sourcemaking.com/design_patterns/singleton/cpp/1 class GlobalClass { private: int m_value; static GlobalClass
I have the following code: Some functions: A::A(int i_a) {cout<<int Ctor\n;} //conversion constructor void
I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
I have the following code int ParseData(unsigned char *packet, int len) { struct ethhdr
Consider the following code: int main() { int *p; ++((int){5}); //compile without err/warning &((int){5});
I have the following code int main() { int a=6; void *p; p=&a; p++;
Consider the following code: int main() { int i; volatile int* p = &i;
Given the following code: int *a = NULL; a = calloc(1, sizeof(*a)); printf(%d\n, a);

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.