473,324 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Where is a static variable stored?

Thanks.

Jul 9 '06 #1
18 33829
"Jack" <ju******@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) ks***@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
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

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
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
* 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
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
>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
"Jack" <ju******@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) ks***@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

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

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
In article <11*********************@35g2000cwc.googlegroups.c om>,
Jack <ju******@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
On 14 Jul 2006 11:27:54 -0700, "Jack" <ju******@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
"Jack" <ju******@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) ks***@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

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
In article <11*********************@35g2000cwc.googlegroups.c om>,
Jack <ju******@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
"Jack" <ju******@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
"Malcolm" <re*******@btinternet.comwrites:
"Jack" <ju******@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) ks***@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
On 14 Jul 2006 11:27:54 -0700, "Jack" <ju******@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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: VJ | last post by:
Hi, I apologize if this question has been asked many times on the group! I am new to programming, I know that there are three section in address space- one for code, one for stack, and the...
3
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
9
by: Peng Jian | last post by:
I have a function that is called very very often. Can I improve its efficiency by declaring its local variables to be static?
10
by: Rene | last post by:
I jus realized that I can change the values of "static variables" and "instance variable" through the standard constructor This means that something like this will compile: public class...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
6
by: junw2000 | last post by:
When I define a static variable, where is the memory allocated for the static variable? Thanks. Jack
2
by: akila15 | last post by:
Where are the static variable getting stored? Is it on heap .. or any other location?
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.