Static Variables

A

akhil.misra

where does a static variable inside a function get stored?? where does
it go in memory?? like Global goes in RAM, Local variable goes on
Stack... but how does static variable takes place in memory??
 
I

Ian Collins

where does a static variable inside a function get stored?? where does
it go in memory?? like Global goes in RAM, Local variable goes on
Stack... but how does static variable takes place in memory??
It's an implementation issue. If the detail is important for your
application, consult your compiler's documentation.
 
R

Richard Heathfield

(e-mail address removed) said:
where does a static variable inside a function get stored??

In random access memory, or "RAM" (or, if it's const, it might get put
into read-only memory, or "ROM").
where does it go in memory??

That's up to the implementation. It varies.
like Global goes in RAM,

Assuming you mean file scope objects with external linkage, yes, they're
stored in RAM (unless they're const, in which case they might go into
ROM).
Local variable goes on Stack...

Assuming you mean automatic objects, they're stored in RAM, unless
they're const, in which case they might go into ROM. They might be
incorporated into some kind of a stack structure, or they might not.
It's up to the implementation.
but how does static variable takes place in memory??

It goes into RAM (or, if it's const, it might go into ROM). The precise
details depend on the implementation.
 
A

akm.satyam

Compiler manual is not mentioning anything about the storage.

What do u think can be the best possible solution for static variable
storage?
 
R

Richard Tobin

where does a static variable inside a function get stored?? where does
it go in memory?? like Global

The only difference between static variable in functions and global
variables is the scope of their names. In particular like globals
they are all known at compile (or link) time. So they can be stored
in the same part of memory as global variables.

(In old-fashioned languages that didn't allow recursion, the same was
true of all variables. There could only be one active instance of a
function, so its variables could be stored in fixed locations. This
was true of early Fortrans, for example.)

-- Richard
 
N

Nick Keighley

please don'e top-post. Put your reply below what you are
replying to, better yet intersperse your reply amongst
the original post. I have re-arranged your post.


Stack *is* RAM.

It's an implementation issue. If the detail is important for your
application, consult your compiler's documentation.

Compiler manual is not mentioning anything about the storage.

well mostly you don't care. You might want to try a compiler
specific ng. It's even possible the internal details of the
compiler arn't publicly documented.

What do u think can be the best possible solution for static variable
storage?

there probably is no "best" solution. The memory must
be accessible by the function and have a lifetime the same
as the program. It is initialised before it is accessed for t
he first time. It is common for all statics (local and global)
to be in the same lump of memory.

Why do you need to know?
 
F

Flash Gordon

Richard Heathfield wrote, On 20/02/07 09:57:
(e-mail address removed) said:


In random access memory, or "RAM" (or, if it's const, it might get put
into read-only memory, or "ROM").

You mean my implementation using pairs of goldfish to store each bit
(one a second the goldfish that knows the current value tells the other
goldfish to "refresh" the bit and stop it from being forgotten) is not a
conforming implementation? Damn.

Assuming you mean automatic objects, they're stored in RAM, unless
they're const, in which case they might go into ROM. They might be
incorporated into some kind of a stack structure, or they might not.
It's up to the implementation.

Or they might go in to non-random-access registers, or might be
completely eliminated by the optimiser.
It goes into RAM (or, if it's const, it might go into ROM). The precise
details depend on the implementation.

I do, of course, agree with the general thrust of your post :)
 
M

matevzb

Richard Heathfield wrote, On 20/02/07 09:57:



You mean my implementation using pairs of goldfish to store each bit
(one a second the goldfish that knows the current value tells the other
goldfish to "refresh" the bit and stop it from being forgotten) is not a
conforming implementation? Damn.
<OT>
Um, you do know the 3-second-memory is a myth don't you? Three months
or so is more like it, which may be a longer period than some people
can handle =)
Or they might go in to non-random-access registers, or might be
completely eliminated by the optimiser.



I do, of course, agree with the general thrust of your post :)
As do I with yours, "except for little fish..."
 
S

santosh

where does a static variable inside a function get stored?? where does
it go in memory?? like Global goes in RAM, Local variable goes on
Stack... but how does static variable takes place in memory??

The C Standard does not impose any specific constraints for the
location of objects. As long as they meet the properties specified in
their declaration, the compiler is free to place them wherever it
wants.

Static objects are essentially a compromise between unqualified file
scope objects, (global), on the one hand and unqualified block scope
objects, (local), on the other hand. While they persist throughout the
program's lifetime, they're accessible only from their scope, (unless
a pointer is used). That implies that it's pretty much out of question
to place them on a stack, (except if it's declared within the main
function).

<OT>
In most implementation statics and other file scope objects are
usually placed in a section of memory called static storage. String
literals might also be placed here, or they might be placed in a read-
only area of storage. But such details are implementation specific,
you should not rely on them if you want your code to be portable. In
most cases the location of objects is quite unneccessary to know.
</OT>
 
K

Keith Thompson

Flash Gordon said:
Richard Heathfield wrote, On 20/02/07 09:57:

You mean my implementation using pairs of goldfish to store each bit
(one a second the goldfish that knows the current value tells the other
goldfish to "refresh" the bit and stop it from being forgotten) is not a
conforming implementation? Damn.
[...]

Sure it's conforming. There's nothing that says that random access
memory has to be made from semiconductors rather than, say, small
fishes or tiny iron toruses. But your access is likely to be more
random than you really want it to be.
 
M

Martin Ambuhl

Compiler manual is not mentioning anything about the storage.

What do u think can be the best possible solution for static variable
storage?

None us knows what this mysterious person 'u' thinks about anything.
For correct agreement with the name 'u', the verb should be third-person
singular, 'thinks'.
 
S

santosh

(e-mail address removed) wrote:

Please don't top post. You're reply should be interspersed with or
below the quoted message. Also don't quote sigs unless you're actually
discussing it.
[corrected here]
Compiler manual is not mentioning anything about the storage.

That is because compiler manuals are meant for the language user. In
almost all cases a language user should not need to know about the
details of the implementation except those required by the language's
standard or those needed to use it.

Unless you're a compiler implementor or are writing specialised
programmes, you do not need to know where exactly in memory C's
objects are placed.
What do u think can be the best possible solution for static variable storage?

The answer to that would depend on the machine to which you're
targetting your C implementation. For more discussions post to
comp.arch or comp.compilers.
 
C

CBFalconer

Compiler manual is not mentioning anything about the storage.

What do u think can be the best possible solution for static
variable storage?

That is a decision made by the compiler system writer.

Please don't top-post. I fixed this one. Your answer belongs
after, or intermixed with, the material to which you reply, after
snipping anything irrelevant. Also, please do not use childish
abbreviations, such as 'u', which only make your post hard to
read. See the following links.

--
Some informative links:
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/> (taming google)
<http://members.fortunecity.com/nnqweb/> (newusers)
 
K

Kenny McCormack

None us knows what this mysterious person 'u' thinks about anything.
For correct agreement with the name 'u', the verb should be third-person
singular, 'thinks'.

It's been a long time since the former UN secretary general has posted
to clc.

But, in any case, ITYM:
What does U think ... ?

I don't think you meant to say "thinks".

P.S. Still, I have to wonder why people get hyper about "u" and other
bits of SMS/IRC-speak, and yet say nothing about the mangling of the
English language found in:

Compiler manual is not mentioning anything about the storage.

I don't know about u, but that sentence hurts my ears a *lot* more than
the use of "phonetic spelling".
 
J

Jack Klein

(e-mail address removed) said:


In random access memory, or "RAM" (or, if it's const, it might get put
into read-only memory, or "ROM").

[snip]

Or flash, or FRAM, or EEPROM, or...
 
R

Richard Heathfield

Jack Klein said:
(e-mail address removed) said:


In random access memory, or "RAM" (or, if it's const, it might get
put into read-only memory, or "ROM").

[snip]

Or flash, or FRAM, or EEPROM, or...

....pigeon guano? A sheet of A4 on my desk (perish the thought)? Yes, the
possibilities are endless.
 
S

Stephen Sprunk

Kenny McCormack said:
P.S. Still, I have to wonder why people get hyper about "u" and other
bits of SMS/IRC-speak, and yet say nothing about the mangling of the
English language found in:

Compiler manual is not mentioning anything about the storage.

I don't know about u, but that sentence hurts my ears a *lot* more than
the use of "phonetic spelling".

There is a difference to many of us between simple laziness (typing "u"
instead of "you" when you know the difference) and someone who is obviously
not a native speaker doing their best but not getting it quite right. Plus,
English grammar is so flexible that it's hard to prove that your example is
even wrong.

The use of "doubt" when people mean "question" grates on my nerves far more
than questionable word order... The two are very distinct concepts in
English, though I can understand that they might not be in another language.

S
 
K

Kenny McCormack

There is a difference to many of us between simple laziness (typing "u"
instead of "you" when you know the difference) and someone who is obviously
not a native speaker doing their best but not getting it quite right.

But, you see, that's the funny thing. That's the thing upon which
reasonable people can disagree. Personally, I accept laziness - keep in
mind that Larry Wall considers it a supreme virtue (and, no, I'm not a
Perl-head) - but "wrongness" is a sin. And that view does hold
considerable weight in this NG, mind you.

The real point, of course, is that no one on Usenet wants to risk being
branded a (drum roll please) "racist". That the one piece of mud that
if you ever let it stick, you're a goner.
Plus, English grammar is so flexible that it's hard to prove that your
example is even wrong.

It's wrong. Trust me.
The use of "doubt" when people mean "question" grates on my nerves far more
than questionable word order... The two are very distinct concepts in
English, though I can understand that they might not be in another language.

I agree; I also think the sentence above is "just plain wrong".
But again, whenever this is raised, people get nervous and back away,
because of the "racist" boogie man.
 
M

Malcolm McLean

where does a static variable inside a function get stored?? where does
it go in memory?? like Global goes in RAM, Local variable goes on
Stack... but how does static variable takes place in memory??
static variables that are local to a function have to persist when that
function is exited. Globals also persist for the duration of the program.

The only difference between them is access control, which is normally
applied at compile time rather than runtime. Almost always, therefore, the
compiler will reserve a chunk of memory for persistent objects and put it
somewhere convenient, like immediately above the stack.

However the exact details are compiler dependent. For instance a lot of
compilers will make a distinction between variables initialised to zero and
those intialised to constants.
 

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,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top