473,405 Members | 2,344 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,405 software developers and data experts.

Static Variables

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
18 3809
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
"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
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

<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: James | last post by:
Hello Java NG, I not sure if this is the right NG for this type of question but if not please let me know which is, TIA Any way first off let me say I'm a student and this WAS last weeks lab,...
2
by: katekukku | last post by:
HI, Could anyone please tell me what are static variables and what exactly are there features. I am a little bit confused. Thank You
9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
4
by: Dave | last post by:
I used the following class and .aspx code below to understand how static works on variables and methods taken from...
8
by: Simone Chiaretta | last post by:
I've a very strange behaveour related to a website we built: from times to times, something should happen on the server, and all static variables inside the web application, both defined inside aspx...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
16
by: RB | last post by:
Hi clever people :-) I've noticed a lot of people stating not to use static variables with ASP.NET, and, as I understand it, the reason is because the variable is shared across user sessions -...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.