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 4017320
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:50:48+00:00 2026-05-20T09:50:48+00:00

Static variables exist outside of the function, in terms of their memory at least

  • 0

Static variables exist outside of the function, in terms of their memory at least (not scope), right? But one thing that always concerned me, is what happens when I call the function a second time. For instance:

f(){
    static char buffer[256*256];
    stuff(buffer);
}

When I call this function a second time, wouldn’t it technically be declaring the variable ‘buffer’ a second time? Or does it work differently with static variables (as opposed to normal ones) once everything is compiled?

… I sometimes wish there was a chart or something of what a c++ compiler usually turns code into (minus optimizations) so I wouldn’t have to bother you fine folks with little questions like this, aha. Thank you in advance!

edit: I know it works like this, I just want to know why though. It’s probably something mind numbingly simple…

  • 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-20T09:50:49+00:00Added an answer on May 20, 2026 at 9:50 am

    Static storage duration objects in function scope.

    These objects are created on first use.
    Then destroyed in reverse order of creation (with other static storage duration objects).

    #include <iostream>
    
    class X
    {
        public:
            X(int x): m(x)      {std::cout << "X: " << m << " created\n"; }
            ~X()                {std::cout << "X: " << m << " destroyed\n";}
    
        private:
            int m;
    };
    
    
    static X    x1(1);
    
    int test()
    {
        std::cout << "Test: Start\n";
        static  X x3(3);
    
        std::cout << "Test: Finished\n";
        return 5;
    }
    
    
    int main()
    {
        std::cout << "Main: Start\n";
        X   x2(2);
    
        test();
    
        X   x4(4);
        std::cout << "Main: Finished\n";
    }
    

    Now Try it: (comments added). SSDO => Static Storage Duration object.

    g++ X.cpp
    ./a.out
    X: 1 created    // SSDO file scope.
    Main: Start
    X: 2 created
    Test: Start
    X: 3 created    // SSDO created on first use (Notice not destroyed)
    Test: Finished
    Test: Start     // Notice not created here.
    Test: Finished
    X: 4 created
    Main: Finished
    X: 4 destroyed
    X: 2 destroyed  // Main now really finished. after destroying local variables.
    X: 3 destroyed  // Destroy SSDO in reverse order of creation. (3 - 1)
    X: 1 destroyed
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.