2

Lately I tried getting my head around the static keyword and here I attempt to simply use the static keyword for a variable declaration within a function. Like so:

void counter()                                                                                        
{                                                                                                     
    static int counter = 0; //should be initialized only once and once only                           
    counter++; //increment for every call of this function                                            
}

I understand that due to the variable being static, it will live outside the function and therefore from wherever I decide to print out counter, it should give me the number of times the function counter() was called. So I did a simple test as shown:

int main()                                                                                            
{                                                                                                     
    for(unsigned int i = 0; i < 10; i++){                                                             
        counter();                                                                                    
    }                                                                                                 
    std::cout << counter << std::endl;                                                                

    return 0;                                                                                         
}

From this test I expected to get number 10... but instead the number of counts the code returned was 1.

Please, what am I missing here?

I found other submissions to "similar" issues such as this one: static counter in c++ But they mostly revolve around the static keyword being used in classes.

miro_x
  • 201
  • 3
  • 8
  • `std::cout << counter` versus `std::cout << counter()`. Just a typographical error. – Dark Falcon Nov 17 '17 at 01:42
  • But I was not trying to print the function `counter()` that would return nothing as its void, instead I'm printing the static variable `counter` defined in the function `counter()` – miro_x Nov 17 '17 at 01:45
  • 2
    The static variable `counter` is accessible only inside the function `counter()`. Outside of it, the **identifier** `counter` refers to the function. – Sam Varshavchik Nov 17 '17 at 01:46
  • 1
    You are not printing `counter`. See this answer https://stackoverflow.com/a/2064722/1143634 –  Nov 17 '17 at 01:46
  • a static local variable can only be called in that local scope, it cannot be called outside. – Lorence Hernandez Nov 17 '17 at 01:46
  • Thank you for the quick replies. I assumed from the C++ documentation I read that static variables remain in memory once initialised and that they remain in memory until the program terminates. But I guess that means I can't just violate the rules of namespaces like I just did. – miro_x Nov 17 '17 at 01:50
  • 2
    Whether or not something "remains in memory" is completely independent of what the scope of the object's identifier is. – Sam Varshavchik Nov 17 '17 at 01:51
  • So in my code what is the number 1 that it returns, it seems from the link that @Ivan gave I am just accessing the function reference? So the function reference is 1? I am still not very clear on that front. – miro_x Nov 17 '17 at 01:55
  • 1
    @mira_x you are trying to print function address, but it cannot be simply printed using `std::ostream`, so it is converted to `bool` implicitly. So your code essentially prints `true` all the time. GCC will warn you if you enable warnings `-Wall -Wextra`. –  Nov 17 '17 at 01:58
  • I see, ok thank you @Ivan – miro_x Nov 17 '17 at 02:03

2 Answers2

2

counter is still a local variable inside void counter() so you still need to return it.

Ahmed Karaman
  • 523
  • 3
  • 12
2

You are printing counter in main(), where it wasn't declared. Just because you declared it as static in counter() doesn't mean you can access it in main().

If you do :

void counter() {
    static int counter = 0;
    std::cout << counter++ << std::endl;
}

int main() {
    for(unsigned int i = 0; i < 10; i++){
        counter();
    }
    return 0;
}

You can see the counter incrementing.

SegFault
  • 2,526
  • 4
  • 21
  • 41