How Static variable persists/retains its value.?
-
Hi, I need some clarification on one point. We normally use static variables in C/C++. We say when we have used a static variable inside a function, it persists its previous value when that function is called again & again, e:g: to say, static variable retains its last value when that function is called again. Can anybody explaing me the logic behind this. How a static variable knows its last value and how it is not initialized everytime on the function call. Regards, Mbatra
-
Hi, I need some clarification on one point. We normally use static variables in C/C++. We say when we have used a static variable inside a function, it persists its previous value when that function is called again & again, e:g: to say, static variable retains its last value when that function is called again. Can anybody explaing me the logic behind this. How a static variable knows its last value and how it is not initialized everytime on the function call. Regards, Mbatra
Because a static variable, even when declared in side a func, is created in the DATA section of a programs run space. Think of it like a global static variable that gets initialised to zero and whose value is of course persistent. An ordinary variable declared inside a func is created on the stack. This of course winds back and forwards so when the func returns the space that variable used is lost to the next func you call.
-
Hi, I need some clarification on one point. We normally use static variables in C/C++. We say when we have used a static variable inside a function, it persists its previous value when that function is called again & again, e:g: to say, static variable retains its last value when that function is called again. Can anybody explaing me the logic behind this. How a static variable knows its last value and how it is not initialized everytime on the function call. Regards, Mbatra
Read the discussion. refer the link below. http://stackoverflow.com/questions/898432/how-is-static-variable-initialization-implemented-by-the-compiler
-
Read the discussion. refer the link below. http://stackoverflow.com/questions/898432/how-is-static-variable-initialization-implemented-by-the-compiler
Stackoverflow? The competition? Are you mad? :)
-
Stackoverflow? The competition? Are you mad? :)
-
Why? Are you saying I am wrong? :)