Thanks.
Where is a static variable stored?
Collapse
This topic is closed.
X
X
-
JackTags: None -
Keith Thompson
Re: Where is a static variable stored?
"Jack" <junw2000@gmail .comwrites:Please put the question in the body of your article. Not allThanks.
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_Keit h) 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.
-
Michael Mair
Re: Where is a static variable stored?
Jack schrieb:You're welcome.Thanks.
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.
Comment
-
Jack
Re: Where is a static variable stored?
Michael Mair wrote:Thanks. Is a static variable stored at heap or stack?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.
Jack
Comment
-
Ian Collins
Re: Where is a static variable stored?
Jack wrote:Please don't quote signatures.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
>
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.
Comment
-
Alf P. Steinbach
Re: Where is a static variable stored?
* Jack:Heap and stack are (in this context) two memory management schemes: ways>
Thanks. Is a static variable stored at heap or stack?
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?
Comment
-
Peter Nilsson
Re: Where is a static variable stored?
Jack wrote:Quite a number of newbies fall into the trap of thinking that _where_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?
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
Comment
-
Gordon Burditt
Re: Where is a static variable stored?
>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
Comment
-
Keith Thompson
Re: Where is a static variable stored?
"Jack" <junw2000@gmail .comwrites:
[...]Probably not.Thanks. Is a static variable stored at heap or stack?
>
Jack
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_Keit h) 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.
Comment
-
jjf@bcs.org.uk
Re: Where is a static variable stored?
Jack wrote:Possibly, or possibly somewhere else. Why do you care? C doesn't>
Thanks. Is a static variable stored at heap or stack?
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.
Comment
-
Jack
Re: Where is a static variable stored?
Michael Mair wrote:I think for a static variable defined out of any functions, i.e., it isJack 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.
>
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.
Comment
-
Walter Roberson
Re: Where is a static variable stored?
In article <1152901674.865 652.57980@35g20 00cwc.googlegro ups.com>,
Jack <junw2000@gmail .comwrote:
Not all compilers -have- "data segments"; not all systems use a "stack".>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?
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. *Implementation s* 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
Comment
-
Barry Schwarz
Re: Where is a static variable stored?
On 14 Jul 2006 11:27:54 -0700, "Jack" <junw2000@gmail .comwrote:
C does not define a data segment or a stack. My system has neither.>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?
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
Comment
-
Keith Thompson
Re: Where is a static variable stored?
"Jack" <junw2000@gmail .comwrites:Once again: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?
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_Keit h) 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.
Comment
-
jjf@bcs.org.uk
Re: Where is a static variable stored?
Jack wrote:You were given several complete and correct answers to this 5 days ago.Michael Mair wrote:>Jack schrieb:You're welcome.Thanks.
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?
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.
Comment
Comment