By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

Static Variables

P: n/a
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??

Feb 20 '07 #1
Share this Question
Share on Google+
18 Replies


P: n/a
ak*********@gmail.com wrote:
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.

--
Ian Collins.
Feb 20 '07 #2

P: n/a
ak*********@gmail.com 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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 20 '07 #3

P: n/a
Compiler manual is not mentioning anything about the storage.

What do u think can be the best possible solution for static variable
storage?
Ian Collins wrote:
ak*********@gmail.com wrote:
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.

--
Ian Collins.
Feb 20 '07 #4

P: n/a
In article <11*********************@m58g2000cwm.googlegroups. com>,
ak*********@gmail.com <ak*********@gmail.comwrote:
>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 20 '07 #5

P: n/a
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.

On 20 Feb, 10:01, akm.sat...@gmail.com wrote:
Ian Collins wrote:
akhil.mi...@gmail.com wrote:
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??
Stack *is* RAM.

It's an implementation issue. If the detail is important for your
application, consult your compiler's documentation.
--
Ian Collins
please remove sigs (the bit after --<space>)

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?
--
Nick Keighley

Feb 20 '07 #6

P: n/a
Richard Heathfield wrote, On 20/02/07 09:57:
ak*********@gmail.com 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").
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.

<snip>
>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.
Or they might go in to non-random-access registers, or might be
completely eliminated by the optimiser.
>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.
I do, of course, agree with the general thrust of your post :-)
--
Flash Gordon
Feb 20 '07 #7

P: n/a
On Feb 20, 8:23 pm, Flash Gordon <s...@flash-gordon.me.ukwrote:
Richard Heathfield wrote, On 20/02/07 09:57:
akhil.mi...@gmail.com 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").

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 =)
</OT>
<snip>
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.

Or they might go in to non-random-access registers, or might be
completely eliminated by the optimiser.
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.

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

Feb 20 '07 #8

P: n/a
ak*********@gmail.com wrote:
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>

Feb 20 '07 #9

P: n/a
Flash Gordon <sp**@flash-gordon.me.ukwrites:
Richard Heathfield wrote, On 20/02/07 09:57:
>ak*********@gmail.com 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").

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.

--
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.
Feb 20 '07 #10

P: n/a
ak********@gmail.com wrote:
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'.
Feb 20 '07 #11

P: n/a
ak********@gmail.com 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]
Ian Collins wrote:
ak*********@gmail.com wrote:
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.

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.

Feb 20 '07 #12

P: n/a
In article <54*************@mid.individual.net>,
Martin Ambuhl <ma*****@earthlink.netwrote:
>ak********@gmail.com wrote:
>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'.
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".

Feb 20 '07 #13

P: n/a
ak********@gmail.com wrote: *** Rude top-posting fixed ***
Ian Collins wrote:
>ak*********@gmail.com wrote:
>>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.

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)
Feb 21 '07 #14

P: n/a
On Tue, 20 Feb 2007 09:57:28 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote in comp.lang.c:
ak*********@gmail.com 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").
[snip]

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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 21 '07 #15

P: n/a
Jack Klein said:
On Tue, 20 Feb 2007 09:57:28 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote in comp.lang.c:
>ak*********@gmail.com 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").

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

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 21 '07 #16

P: n/a
"Kenny McCormack" <ga*****@xmission.xmission.comwrote in message
news:er**********@news.xmission.com...
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

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov

--
Posted via a free Usenet account from http://www.teranews.com

Feb 21 '07 #17

P: n/a
In article <45***********************@free.teranews.com>,
Stephen Sprunk <st*****@sprunk.orgwrote:
>"Kenny McCormack" <ga*****@xmission.xmission.comwrote in message
news:er**********@news.xmission.com...
>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.
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.

Feb 21 '07 #18

P: n/a

<ak*********@gmail.comwrote in message
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.

Feb 21 '07 #19

This discussion thread is closed

Replies have been disabled for this discussion.