Re: Where is a static variable stored?
- From: "Malcolm" <regniztar@xxxxxxxxxxxxxx>
- Date: Sat, 15 Jul 2006 05:51:29 +0100
"Jack" <junw2000@xxxxxxxxx> wrote
I think for a static variable defined out of any functions, i.e., it isA static local variable is basically a global which is only visible within
a global variable, it is located in the data segment of the program.
How about a static variable defined within a function? it is a local
variable. Is it located at the stack?
one function.
That is a contradiction in terms, but it is how the compiler will treat it
under the bonnet.
As for data segment, only some OSes have such things. When the program
loads, memory does need to be reserved for all the global variables,
including the static locals, which is why you can think of them as local
globals.
Incidentally this has an important effect. Let's say we need 1000 xyz
coordinates
/* dangerous, may overflow stack if you are not careful */
void foo()
{
double coords[1000][3];
}
/* safe, memory will be allocated at load time. But gobbles resources for
other functions */
void foo()
{
static double coords[1000][3];
}
--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm
.
- Follow-Ups:
- Re: Where is a static variable stored?
- From: Keith Thompson
- Re: Where is a static variable stored?
- References:
- Where is a static variable stored?
- From: Jack
- Re: Where is a static variable stored?
- From: Michael Mair
- Re: Where is a static variable stored?
- From: Jack
- Where is a static variable stored?
- Prev by Date: Re: good C compiler
- Next by Date: Re: After read a whole OS written by C, someone asks `What is the advantage of C'
- Previous by thread: Re: Where is a static variable stored?
- Next by thread: Re: Where is a static variable stored?
- Index(es):
Relevant Pages
|