0

can someone exlpain this code to me

size_t count_calls() {    
static size_t ctr = 0;  // value will persist across calls

    return ++ctr; 
}
int main() {    
for (size_t i = 0; i != 10; ++i) 
       cout << count_calls() << endl; 
}

how can ctr not restet to zero after calling the count calls function many times? As you can see the object ctr intelizes to 0 after the function call. shouldnt ctr be 0 when reentering the function count_calls as it passes the first line static size_t ctr = 0;?

I understand that a static variable holds its value thoughout the function calls until the program terminates but here, every time the functions is called shouldnt the first linee reset the the ctr value to zero or does the intialization work only the first time and thus become ignored the rest of the calls?

can someone please explain how this works?

swan
  • 1
  • 1
  • Static objects inside functions are initialized the first time the function is called. Also, you don’t need the extra stuff that `std::endl` does; `’\n’` ends a line. – Pete Becker Jan 26 '20 at 01:07
  • Yes I understand that but what happens the second, thirds ... times the program calls the function count_call()? does the first line static size_t ctr = 0; get ignored by the compiler upon multiable calls? That is my actual guestion. thanks for clarufication – swan Jan 26 '20 at 12:42
  • endl is used to clear the iostream buffer, while \n end a line and start a new one. It is good practice to have endl to clear the buffer in larger programs so you dont have something stored from previus calls. – swan Jan 26 '20 at 12:47
  • No, indiscriminately flushing the output stream is a major performance hit, and rarely accomplishes anything useful. It really doesn't matter if you have something stored from previous calls; anything in the buffer will be flushed at program termination, and if you're doing interactive I/O, the input operation will (unless you've changed the defaults) flush the output buffer. Automatically flushing the buffer every time you write a line is simply a performance hit. If you've done that and someone redirects stdout to a file you'll get lots of complaints about how slow your program is. – Pete Becker Jan 26 '20 at 16:53
  • There is one situation where flushing the output buffer after every output operation could be helpful: when you're trying to debug a program that crashes mysteriously and you're adding output statements to show you where you are. But when you're doing that you should probably be writing to `std::cerr` which, as the name suggests, is for error output. It flushes the buffer on each line of output. – Pete Becker Jan 26 '20 at 16:59
  • Oh thanks, that was very insightful of you! I will use \n from now on then. Many thanks! Also I found the answer to the first question. After the first intialization of a static variable the complier will ignor the after additonal initialization and thus would not care if a static variable is initilized again. It will just operate on the stored value. In my case after the secound call to count_call() the complier will ignor the first line and goes direktly to incriment ctr. Please correct me if I missed on something? :) – swan Jan 27 '20 at 14:58

0 Answers0