398,054 Members | 1,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 398,054 IT Pros & Developers. It's quick & easy.

Where is a static variable stored?

Jack
P: n/a
Thanks.

Jul 9 '06 #1
Share this Question
Share on Google+
18 Replies


Keith Thompson
P: n/a
"Jack" <junw2000@gmail.comwrites:
Thanks.
Please put the question in the body of your article. Not all
newsreaders display the subject along with the body.

The subject was: "Where is a static variable stored?".

The answer: In memory. A variable of static storage duration must be
available throughout the lifetime of the program. The C standard
doesn't require any specific method of making this happen, and
different compilers can do it differently.

If you want to know how some particular compiler does this, you'll
need to consult your compiler's documentation or ask in a
compiler-specific or system-specific newsgroup -- but any code you
write that depends on this information will be non-portable, and
probably needlessly so.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 9 '06 #2

Michael Mair
P: n/a
Jack schrieb:
Thanks.
You're welcome.

Please write/repeat your question in the message text -- there
are newsreaders which do not show subject and message text at
the same time.

Your question: "Where is a static variable stored?" has no
standard C answer.
Variables with static storage duration "live" throughout the
programme's lifetime. Whether they are stored all or only part
of the time in RAM, ROM, or registers is not specified -- they
have only to behave as if they were there the whole time.

This may not be the answer to what you _wanted_ to ask; please
be precise in your questions.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jul 9 '06 #3

Jack
P: n/a

Michael Mair wrote:
Jack schrieb:
Thanks.

You're welcome.

Please write/repeat your question in the message text -- there
are newsreaders which do not show subject and message text at
the same time.

Your question: "Where is a static variable stored?" has no
standard C answer.
Variables with static storage duration "live" throughout the
programme's lifetime. Whether they are stored all or only part
of the time in RAM, ROM, or registers is not specified -- they
have only to behave as if they were there the whole time.

This may not be the answer to what you _wanted_ to ask; please
be precise in your questions.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Thanks. Is a static variable stored at heap or stack?

Jack

Jul 9 '06 #4

Ian Collins
P: n/a
Jack wrote:
Michael Mair wrote:
>>Jack schrieb:
>>>Thanks.

You're welcome.

Please write/repeat your question in the message text -- there
are newsreaders which do not show subject and message text at
the same time.

Your question: "Where is a static variable stored?" has no
standard C answer.
Variables with static storage duration "live" throughout the
programme's lifetime. Whether they are stored all or only part
of the time in RAM, ROM, or registers is not specified -- they
have only to behave as if they were there the whole time.

This may not be the answer to what you _wanted_ to ask; please
be precise in your questions.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Thanks. Is a static variable stored at heap or stack?

Jack
Please don't quote signatures.

If your implementation is a typical managed one that uses heap and
stack, static variables won't be on either.

They will be stored somewhere else in the program's address space,
frequently another memory segment.

But there isn't a standard answer.

--
Ian Collins.
Jul 9 '06 #5

Alf P. Steinbach
P: n/a
* Jack:
>
Thanks. Is a static variable stored at heap or stack?
Heap and stack are (in this context) two memory management schemes: ways
to allocate and deallocate memory. They are not, necessarily, two areas
of memory, and furthermore the C language standard probably does not use
the terms heap and stack. For a given C implementation it may be the
case that there is an area of memory used for stack allocation (last
allocated first deallocated), and an area of memory used for heap
allocation (arbitrary order of allocation and deallocation), and if so
it's highly unlikely, but not impossible, that a static variable resides
in one of these memory areas; what you do know is what's already been
explained in this thread about the /lifetime/ of a static variable.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 9 '06 #6

Peter Nilsson
P: n/a
Jack wrote:
Michael Mair wrote:
...
Your question: "Where is a static variable stored?" has no
standard C answer.
Variables with static storage duration "live" throughout the
programme's lifetime. Whether they are stored all or only part
of the time in RAM, ROM, or registers is not specified -- they
have only to behave as if they were there the whole time.

Thanks. Is a static variable stored at heap or stack?
Quite a number of newbies fall into the trap of thinking that _where_
is more important than _when_ when it comes to object storage.
You appear to be one of them.

The standard says nothing about where, it only says when. That's
all the vast majority of C programs ever need to know.

--
Peter

Jul 9 '06 #7

Gordon Burditt
P: n/a
>Thanks. Is a static variable stored at heap or stack?

On many implementations, NO.

Heap: that place from which dynamically allocated memory (from
malloc() and friends) is allocated.
Stack: that place from which auto variables are allocated, even
if there is no hardware stack.

"heap" and "stack" need not be mutually exclusive, using the above
definitions.

Gordon L. Burditt
Jul 9 '06 #8

Keith Thompson
P: n/a
"Jack" <junw2000@gmail.comwrites:
[...]
Thanks. Is a static variable stored at heap or stack?

Jack
Probably not.

Why do you care? If you're just trying to understand how things are
implemented, that's great (and you need to be aware, in this case,
that the language specifies lifetime, not mechanism).

A static variable is stored in memory. It's available throughout the
lifetime of your program, and it has an address that does not vary
during the execution of your program (though it can vary from one
execution to the next). That's really all you need to know.

Well, maybe not. Some systems impose limits on certain memory regions
(stack size, data size, etc), and knowing what's stored where can be
helpful if you're trying to avoid running into those limits.

Tell us what you're really trying to do, and we can probably help you
do it. I suspect you're making some implicit assumptions; without
knowing what those assumptions are, we can't be very helpful.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 9 '06 #9

jjf@bcs.org.uk
P: n/a
jjf

Jack wrote:
>
Thanks. Is a static variable stored at heap or stack?
Possibly, or possibly somewhere else. Why do you care? C doesn't
specify where they are stored. Every compiler could store them
somewhere different, or even store different static variables in a
single program in different ways if it chose to.

If you have a good reason to care, you'll have to check with someone
who knows how your compiler works in the circumstances in which you're
using it - either that, or look at the code it has produced and work it
out from there. Bear in mind that it may not do it that way next time,
and other compilers may not do it that way.

Jul 10 '06 #10

Jack
P: n/a

Michael Mair wrote:
Jack schrieb:
Thanks.

You're welcome.

Please write/repeat your question in the message text -- there
are newsreaders which do not show subject and message text at
the same time.

Your question: "Where is a static variable stored?" has no
standard C answer.
Variables with static storage duration "live" throughout the
programme's lifetime. Whether they are stored all or only part
of the time in RAM, ROM, or registers is not specified -- they
have only to behave as if they were there the whole time.

This may not be the answer to what you _wanted_ to ask; please
be precise in your questions.
I think for a static variable defined out of any functions, i.e., it is
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?

Thanks.

Jul 14 '06 #11

Walter Roberson
P: n/a
In article <1152901674.865652.57980@35g2000cwc.googlegroups.c om>,
Jack <junw2000@gmail.comwrote:
>I think for a static variable defined out of any functions, i.e., it is
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?
Not all compilers -have- "data segments"; not all systems use a "stack".

The C language does not care where static variables get stored, only
that they do. It does not care that all static variables get stored
in the same -kind- of locations -- only that the implementation
knows how to reference them.

The C language makes no distinction of storage location for static
variables declared outside of functions and for static variables
declared inside of functions. *Implementations* of C might make
a distinction... or might not. If you want to know what -your-
implementation does, you need to ask in a newsgroup that discusses
that implementation.

The only distinction the C language makes between static variables
declared outside of functions and those declared inside of functions,
has to do with the scope of the name. static variables declared outside
of functions have a name scope that extends to the end of the
translation unit. static variables declared inside of functions
have a name scope that extends to the end of the block they are
declared in. This distinction in naming scope imposes no barriers
to the implementation chosing to store both kinds of variable
in the same kind of storage: the distinction only has to do with
which parts of the code can directly get a handle to the location.
At the implementation level, there is a minor distinction needed
in the debugging information (if any) that is kept around, but
that's nothing you should be worried about unless you are designing
the compiler or debugger themselves.

--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Jul 14 '06 #12

Barry Schwarz
P: n/a
On 14 Jul 2006 11:27:54 -0700, "Jack" <junw2000@gmail.comwrote:
>I think for a static variable defined out of any functions, i.e., it is
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?
C does not define a data segment or a stack. My system has neither.
Static variables are placed where they can live for the life of the
program. Where that is depends on your hardware, OS, compiler, and
options.
Remove del for email
Jul 14 '06 #13

Keith Thompson
P: n/a
"Jack" <junw2000@gmail.comwrites:
Michael Mair wrote:
>Jack schrieb:
Thanks.

You're welcome.

Please write/repeat your question in the message text -- there
are newsreaders which do not show subject and message text at
the same time.

Your question: "Where is a static variable stored?" has no
standard C answer.
Variables with static storage duration "live" throughout the
programme's lifetime. Whether they are stored all or only part
of the time in RAM, ROM, or registers is not specified -- they
have only to behave as if they were there the whole time.

This may not be the answer to what you _wanted_ to ask; please
be precise in your questions.

I think for a static variable defined out of any functions, i.e., it is
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?
Once again:

Variables with static storage duration "live" throughout the
programme's lifetime. Whether they are stored all or only part
of the time in RAM, ROM, or registers is not specified -- they
have only to behave as if they were there the whole time.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 14 '06 #14

jjf@bcs.org.uk
P: n/a
jjf

Jack wrote:
Michael Mair wrote:
Jack schrieb:
Thanks.
You're welcome.

Please write/repeat your question in the message text -- there
are newsreaders which do not show subject and message text at
the same time.

Your question: "Where is a static variable stored?" has no
standard C answer.
Variables with static storage duration "live" throughout the
programme's lifetime. Whether they are stored all or only part
of the time in RAM, ROM, or registers is not specified -- they
have only to behave as if they were there the whole time.

This may not be the answer to what you _wanted_ to ask; please
be precise in your questions.

I think for a static variable defined out of any functions, i.e., it is
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?
You were given several complete and correct answers to this 5 days ago.
What about the answers do you not understand? The C language does not
define where the data is stored. It can't, because it has to work on
many different types of computers. You can run code written in C on
machines that don't have data segments and don't have stacks - so how
could a variable be stored in either the data segment or the stack in
this case?

Why do you care where it's stored? People need to know this to be able
to tell you what you really want to know.

If you're looking for answers about a particular Operating System
running on a particular type of computer, you need to ask in a group
which discusses that OS on that computer.

Jul 15 '06 #15

Walter Roberson
P: n/a
In article <1152901674.865652.57980@35g2000cwc.googlegroups.c om>,
Jack <junw2000@gmail.comwrote:
>I think for a static variable defined out of any functions, i.e., it is
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?
And if the other more detailed answers are not sufficiently clear
to you:

NO. I have never encountered an implementation that stored
a static variable in the stack. Doing so would not be impossible
(provided the implementation has a stack at all), but it is enough
extra work to be unlikely to be the chosen mechanism; such an
implementation would, for example, have the side effect of being
unable to build dynamically loadable libraries.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Jul 15 '06 #16

Malcolm
P: n/a
"Jack" <junw2000@gmail.comwrote
I think for a static variable defined out of any functions, i.e., it is
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?
A static local variable is basically a global which is only visible within
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
Jul 15 '06 #17

Keith Thompson
P: n/a
"Malcolm" <regniztar@btinternet.comwrites:
"Jack" <junw2000@gmail.comwrote
>I think for a static variable defined out of any functions, i.e., it is
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?
A static local variable is basically a global which is only visible within
one function.
That is a contradiction in terms, but it is how the compiler will treat it
under the bonnet.
If you think about it, it's not really a contradiction in terms.
Visibility and lifetime are two separate things. A static variable
declared inside a function has the same lifetime as any other static
variable; only its visibility *by its name* is restricted to the
function (more precisely, to block scope).

The variable is still accessible throughout the program if the
function exports a pointer to it.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 15 '06 #18

Al Balmer
P: n/a
On 14 Jul 2006 11:27:54 -0700, "Jack" <junw2000@gmail.comwrote:
>
Michael Mair wrote:
>Jack schrieb:
Thanks.

You're welcome.

Please write/repeat your question in the message text -- there
are newsreaders which do not show subject and message text at
the same time.

Your question: "Where is a static variable stored?" has no
standard C answer.
Variables with static storage duration "live" throughout the
programme's lifetime. Whether they are stored all or only part
of the time in RAM, ROM, or registers is not specified -- they
have only to behave as if they were there the whole time.

This may not be the answer to what you _wanted_ to ask; please
be precise in your questions.

I think for a static variable defined out of any functions, i.e., it is
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?
No. Yes. Maybe. Not applicable. Did you even read the previous
answers? Are you having trouble understanding them?

For specific answers about the implementation you're using, you need
to go to a group which deals with that implementation. However, I urge
you not to do that until you truly understand what you've been told
here.

--
Al Balmer
Sun City, AZ
Jul 15 '06 #19

This discussion thread is closed

Replies have been disabled for this discussion.