|
-
October 18th, 2005, 12:37 PM
#1
local static variables
hi,
is it correct to state that a static variable is created only once and initialised only once and occupies some special global memory?
So lets say some sub routines declare and initialise a local static variable of the same name.
Is this possible - I mean will the variables only have local scope to the sub routines they are declared and initialised in ?
Will they exist seperately or how are they allocated?
Code:
void my_func1 ()
{
static int var = 0;
// do whatever
}
void my_func2 ()
{
static int var = 0;
// do whatever
}
int main ()
{
my_func1();
my_func2();
return 0;
}
Last edited by Moore; October 18th, 2005 at 12:52 PM.
-
October 18th, 2005, 01:35 PM
#2
Re: local static variables
life of a static variable declared in a function begins at the time of 1st execution of the function and till the program ends. Yes it's allocated only once and contains the value which was assigned to it in that function while the program is running. In your example, these are two different variables, and they have different addresses in memory. And they are seen only in the functions they are declared in.
Hope this helps some,
Oleg
Last edited by Xatrix; October 18th, 2005 at 01:38 PM.
-
October 18th, 2005, 05:09 PM
#3
Re: local static variables
Just to be clear, a static local is only initialised once - when it's created. If you change its value in the function, it will retain that value on the next call.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
October 18th, 2005, 11:09 PM
#4
Re: local static variables
Appreciate others by rating good posts
"Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett
-
October 19th, 2005, 12:50 AM
#5
Re: local static variables
ok, thanks alot everyone .
-
October 19th, 2005, 01:01 AM
#6
Re: local static variables
 Originally Posted by Moore
hi,
is it correct to state that a static variable is created only once and initialised only once and occupies some special global memory?
So lets say some sub routines declare and initialise a local static variable of the same name.
Is this possible - I mean will the variables only have local scope to the sub routines they are declared and initialised in ?
Will they exist seperately or how are they allocated?
Code:
void my_func1 ()
{
static int var = 0;
// do whatever
}
void my_func2 ()
{
static int var = 0;
// do whatever
}
int main ()
{
my_func1();
my_func2();
return 0;
}
as Graham and Xatrix explained you very well.in your Program u r using two static variable local to the function.
First something regarding static
In both C and C++ the keyword static has two basic meanings, which
unfortunately often step on each other’s toes:
Allocated once at a fixed address; that is, the object is created in a special
static data area rather than on the stack each time a function is called. This is
the concept of static storage.
Local to a particular translation unit (and local to a class scope in C++, as
you will see later). Here, static controls the visibility of a name, so that
name cannot be seen outside the translation unit or class. This also
describes the concept of linkage, which determines what names the linker
will see.
According to your Program==>> When you create a local variable inside a function, the compiler allocates
storage for that variable each time the function is called by moving the stack
pointer down an appropriate amount. If there is an initializer for the variable,
the initialization is performed each time that sequence point is passed.
Sometimes, however, you want to retain a value between function calls. You
could accomplish this by making a global variable, but then that variable
would not be under the sole control of the function. C and C++ allow you to
create a static object inside a function; the storage for this object is not on the
stack but instead in the program’s static data area. This object is initialized
only once, the first time the function is called, and then retains its value
between function invocations.
Last edited by humptydumpty; October 19th, 2005 at 01:10 AM.
-
October 19th, 2005, 01:13 AM
#7
Re: local static variables
hi,
According to your Program==>>
When you create a local variable inside a function, the compiler allocates
storage for that variable each time the function is called by moving the stack
pointer down an appropriate amount. If there is an initializer for the variable,
the initialization is performed each time that sequence point is passed.
So this is a little confusing as it states that the "initialisation is performed each time that sequence point is passed"
so considering the sample program again, are you saying that each time the my_func 's are called, that new storage is allocated and init to 0 is done again? If so how can the "var" retain its value?
thanks.
-
October 19th, 2005, 01:22 AM
#8
Re: local static variables
 Originally Posted by Moore
hi,
So this is a little confusing as it states that the "initialisation is performed each time that sequence point is passed"
so considering the sample program again, are you saying that each time the my_func 's are called, that new storage is allocated and init to 0 is done again? If so how can the "var" retain its value?
thanks.
Nope In case of static variable only Singel TIme Initialization take Place .
have u read this
C and C++ allow you to create a static object inside a function; the storage for this object is not on the stack but instead in the program’s static data area. This object is initialized only once, the first time the function is called, and then retains its value between function invocations.
In Upper Quote i mean to say if this is a Auto variable
have a look on This
According to your Program==>>
When you create a local variable inside a function, the compiler allocates storage for that variable each time the function is called by moving the stack pointer down an appropriate amount. If there is an initializer for the variable, the initialization is performed each time that sequence point is passed.
Last edited by humptydumpty; October 19th, 2005 at 01:26 AM.
-
October 19th, 2005, 01:26 AM
#9
Re: local static variables
yes and that is my understanding, but I thought that the paragragh I quoted from your previous post was a little contradictory to the highlighted red text.
regardless, I pretty much understand it now.
thanks.
-
October 19th, 2005, 01:28 AM
#10
Re: local static variables
 Originally Posted by Moore
yes and that is my understanding, but I thought that the paragragh I quoted from your previous post was a little contradictory to the highlighted red text.
regardless, I pretty much understand it now.
thanks.
u welcome. and sorry to take second time to understand
-
October 19th, 2005, 01:33 AM
#11
Re: local static variables
 Originally Posted by Moore
So lets say some sub routines declare and initialise a local static variable of the same name. Is this possible - I mean will the variables only have local scope to the sub routines they are declared and initialised in ? Will they exist seperately?
I guess that rest of your concerns have been answered. About the existence of two static variables with same name in two different functions in the same translation unit, probably there is some kind of name-mangling to ensure independent and unique existence but I am not sure about how compilers do this actually. And I would be little concerned about it since the compiler does this job for me very well. Hope this helps. Regards.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
October 19th, 2005, 01:38 AM
#12
Re: local static variables
great, thanks again to all.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|