开发者

C++: Static variable declarations in a function

开发者 https://www.devze.com 2023-02-14 12:47 出处:网络
Static variables exist outside of the funct开发者_StackOverflow中文版ion, in terms of their memory at least (not scope), right?But one thing that always concerned me, is what happens when I call the f

Static variables exist outside of the funct开发者_StackOverflow中文版ion, 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...


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


No, you static means that it is outside the scope of your function. It has the same effect as writing:

static char buffer[256*256];

f(){
    stuff(buffer);
}

Except that the buffer is only visible in the scope of your function, and the code is more readable.

(NOTE: My example doesn't apply when char is not a primitive type - in that case, it is constructed the first time it is "declared").


In this context, static means that the variable has application lifetime. It is allocated before main() the function is entered, and deallocated after main() has returned. Also, its value is preserved between function calls. Think of it as a global variable that is only visible from inside that function.


The variable exists before and after you call the function...it's static.


This example might illustrate it:

#include <iostream>
using namespace std;

void test() {
  static int i = 123;
  if (i == 123) {
    i = 321;
  }
  cout << i << endl;
}

int main(int arg, char **argv) {
  test();
  test();
  return 0;
}

The output is:

321

321

So "i" is only initialized the first time it is encountered, so to speak. But actually it's allocated at compile time for that function. Afterwards, it's just in the scope of the function test() as a variable, but it is static so changing it changes it in all future calls to test() as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消