Another simple Function Question

Hi there, I have a simple function question. We stumbled upon "Static Local Variables" and I am confused on what this is supposed to do. I thought making the function just "int var = 10;" will just increment it by 10 but apparently you need to make it "static var = 10;" inorder to increment, I don't understand the logic and reasoning behind this? Thank you.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

void showVar(); // Function prototype

int main()
{
	for (int count = 0; count <= 10; count++)
	{
		showVar();
	}
	return 0;
}

// Definition of function showVar

void showVar()
{
	static int var = 10;
	cout << var << endl;
	var++;
}
1
2
3
4
5
void foo()
{
  int var = 10;
  ++var;
}


var is created and set to 10
var becomes 11
var is destroyed

1
2
3
4
5
void bar()
{
  static int gaz = 10;
  ++gaz;
}


gaz is created and set to 10 only on the first call
gaz increments
gaz is NOT destroyed

Object lifetime.

When the foo() is called, some memory is allocated from stack for the var. The body of foo() uses that memory. When foo() ends, the stack unwinds, releasing the memory. The var is no more. Every call of foo() has its own, distinct var.

When bar() is called, no memory from stack is allocated for the gaz. The program has already on start allocated some other memory for gaz. However, the first call of bar() will initialize the gaz with value 10. Further calls of bar() will not do that, because the gaz has already been initialized.
The gaz is not destroyed/deallocated at the end of bar(). Every call to bar() sees the same memory location that stores the value of gaz.
Can you tell us what static means in this context? I know, but I am getting you to go and look to find out, it's a great way to learn.

Try Google ........
@Keskiverto so to my knowledge, in order to create the proper increment in functions, you need to have static in front of it inorder to increment so the "var" is not destroyed, to my knowledge. If that's the case, then why don't we declare every single variable inside functions to static?

Also, because "gaz" is not destroyed because of the static, it increments. So in other words static is saving the variable in some way? I decided to further test it out and hopefully I got the logic right..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

void foo()
{
    static int x = 5; // So remember, if we do "static" it will actually increment.
    cout << x << endl;
    x++;
}

/*
void foo()
{
    int x = 5; // This will not increment, because nothing is saving the variable "X".
    cout << x << endl;
    x++;
}
*/

int main()
{
    foo();
    foo();
    return 0;
}



@TheIdeasMan your right, I should of searched it first. I stumbled on this https://stackoverflow.com/questions/5033627/static-variable-inside-of-a-function-in-c

which was very useful in regards to my question.
if you made every variable static, you would run out of stack space for large programs AND you risk weird side effects.

consider

int dumsum(int a, int b)
{
static int result = a+b;
return result;
}

...
x = dumsum(3,4); //correct, x = 7
y = dumsum(100,100); //y = 7!!


it is correct without the static. The value is returned, without making it static. Most of the time, using static on a returned value is probably a red flag that your design had a flaw. It works, but its unclean.

having a running increment as static is a good use of static. It keeps its value, and each time you call it, it changes and remembers, that is desirable. Just be careful with multi-threaded code, this may fall apart in race conditions. That is, having 2 copies of the function execute at exactly the same time, they both modify the static value...

void oops()
{
static int duh = 0;
duh++;
sleep(5000);
cout << duh;
}

if you called that in 2 threads at once, it might just print 2 from both threads (each thread starts, each increments duh, so its now 2, sleeps end, and for both threads, duh is 2...)




Last edited on
Make sense now, thanks everyone for the help.
Topic archived. No new replies allowed.