473,588 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Where is a static variable stored?

Thanks.

Jul 9 '06 #1
18 33863
"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_Keit h) 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_Keit h) 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3531
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 third for Heap. I just want to know how and where static members are stored in the address space given to particular program. Is this different from the way it is done in C?
3
3921
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; } /* in above case where is variable k and j mapped in memory layout ? */
9
4605
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
2899
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 SomeClass { public SomeClass() { abc++; // Instance Variable xyz++; // Static Variable
4
14883
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 strongly name which contains the class where the static variable is defined. This library can be referenced by multiple projects. I am fairly sure the static variable does not survive across the application boundry but does it within the application...
6
3959
by: junw2000 | last post by:
When I define a static variable, where is the memory allocated for the static variable? Thanks. Jack
2
3735
by: akila15 | last post by:
Where are the static variable getting stored? Is it on heap .. or any other location?
37
5449
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 are the one which runs statically with the defining class. Hence, my understanding is that static variables must be bound to the class defining the variables and shared by children of parent class where the variable is defined. But, please have a...
11
8303
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 device_t { const device_backend_t *backend; ... } device_t; typedef struct device_backend_t {
0
8220
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8352
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7981
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8222
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5723
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2367
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1457
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1194
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.