Where is a static variable stored?

K

Keith Thompson

Jack said:

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.
 
M

Michael Mair

Jack said:

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
 
J

Jack

Michael said:
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

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

Jack
 
I

Ian Collins

Jack said:
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.
 
A

Alf P. Steinbach

* 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.
 
P

Peter Nilsson

Jack said:
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.
 
G

Gordon Burditt

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
 
K

Keith Thompson

Jack said:
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.
 
J

jjf

Jack said:
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.
 
J

Jack

Michael said:
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.
 
W

Walter Roberson

Jack said:
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.
 
B

Barry Schwarz

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
 
K

Keith Thompson

Jack said:
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.
 
J

jjf

Jack said:
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.
 
W

Walter Roberson

Jack said:
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.
 
M

Malcolm

Jack said:
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];
}
 
K

Keith Thompson

Malcolm said:
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.
 
A

Al Balmer

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top