Dear all,
Where are global , static , and automatic variables stored in memory?
Thanks in advance.
7 43064
Dear all,
Where are global , static , and automatic variables stored in memory?
Thanks in advance.
Hi,
All types of variables are stored in Heap memory (RAM), here I hv mentioned Heap because Heap is portion of RAM which is occupied by your program to save its variables till it runs and once it exists the Heap will be deallocated automatically. Constants are stored in code memory and it cannot be changed, but constants has a very instresting aspects but to understand it you must be well aware of these memories.
Banfa 9,065
Expert Mod 8TB
It depends entirely on what platform you are using.
In an embedded environment, for instance
global/static variables go into different RAM memory segments depending on whether or not they are initialised.
constants are often left in ROM
automatic variables are normally placed of the stack of the currently running task but not always.
The question why would you need to know springs to mind.
Banfa 9,065
Expert Mod 8TB
Copied reply received in PM
I don't know what u mean by your reply to my question
Where are the static , auto,extern stored?
Ur answer was "i dont know why did this question come to u?"
The objective of asking any question is to be clear from
doubts. Hope u got it. Thank you.
Banfa 9,065
Expert Mod 8TB
The point is that the C/C++ standards do not require that any of these types of variable be stored anywhere specifically, the question you have asked can only be answered for a specific platform and since you haven't stated what platform you are using the question is unanswerable.
It is true that lots of different platforms use similar models for there data storage.
As it is extern, static are classes of variables that have nothing to do with the location of the variable in memory but are only related to the scope of the variable in code.
My question was "why do you need to know?" not "why do you want to know?" what I was trying to get at was is there some underlying problem to which you think the answer to this question will provide the solution, in which case tell us that problem, or are you just being inquisitive?
The general solution is to dis assemble any code, we utility to disassemble any binary using tools like visual studio. in compilers like gcc there is a option to compile and assebmle and not link.
In general static variables are stored in BSS,
Example:: -
void MyFunction(int a)
-
{
-
static int x = 0;
-
printf("my number: ");
-
printf("%d, %d\n", a, x);
-
}
-
when compiled and disassembled in windows environment
-
BSS SEGMENT
-
?x@?1??MyFunction@@9@9 DD 01H DUP (?) ; `MyFunction'::`2'::x
-
_BSS ENDS
-
_DATA SEGMENT
-
$SG796 DB 'my number: ', 00H
-
$SG797 DB '%d, %d', 0aH, 00H
-
_DATA ENDS
-
PUBLIC _MyFunction
-
EXTRN _printf:NEAR
-
; Function compile flags: /Odt
-
_TEXT SEGMENT
-
_a$ = 8 ; size = 4
-
_MyFunction PROC NEAR
-
; Line 4
-
push ebp
-
mov ebp, esp
-
; Line 6
-
push OFFSET FLAT:$SG796
-
call _printf
-
add esp, 4
-
; Line 7
-
mov eax, DWORD PTR ?x@?1??MyFunction@@9@9
-
push eax
-
mov ecx, DWORD PTR _a$[ebp]
-
push ecx
-
push OFFSET FLAT:$SG797
-
call _printf
-
add esp, 12 ; 0000000cH
-
; Line 8
-
pop ebp
-
ret 0
-
_MyFunction ENDP
-
_TEXT ENDS
-
Constants :: Compilers might choose to store constants in text section or code section, since these sections are read only.
Dyanmically created variables are usually stored in Heap and other auto variables are stored in Stack.
The reason to store in Stack is stack follows last in first out hence scope of any variables ends as it leaves the scope boundary.
The general solution is to dis assemble any code, we utility to disassemble any binary using tools like visual studio. in compilers like gcc there is a option to compile and assebmle and not link.
In general static variables are stored in BSS,
Example:: -
void MyFunction(int a)
-
{
-
static int x = 0;
-
printf("my number: ");
-
printf("%d, %d\n", a, x);
-
}
-
when compiled and disassembled in windows environment
-
BSS SEGMENT
-
?x@?1??MyFunction@@9@9 DD 01H DUP (?) ; `MyFunction'::`2'::x
-
_BSS ENDS
-
_DATA SEGMENT
-
$SG796 DB 'my number: ', 00H
-
$SG797 DB '%d, %d', 0aH, 00H
-
_DATA ENDS
-
PUBLIC _MyFunction
-
EXTRN _printf:NEAR
-
; Function compile flags: /Odt
-
_TEXT SEGMENT
-
_a$ = 8 ; size = 4
-
_MyFunction PROC NEAR
-
; Line 4
-
push ebp
-
mov ebp, esp
-
; Line 6
-
push OFFSET FLAT:$SG796
-
call _printf
-
add esp, 4
-
; Line 7
-
mov eax, DWORD PTR ?x@?1??MyFunction@@9@9
-
push eax
-
mov ecx, DWORD PTR _a$[ebp]
-
push ecx
-
push OFFSET FLAT:$SG797
-
call _printf
-
add esp, 12 ; 0000000cH
-
; Line 8
-
pop ebp
-
ret 0
-
_MyFunction ENDP
-
_TEXT ENDS
-
Constants :: Compilers might choose to store constants in text section or code section, since these sections are read only.
Dyanmically created variables are usually stored in Heap and other auto variables are stored in Stack.
The reason to store in Stack is stack follows last in first out hence scope of any variables ends as it leaves the scope boundary.
well i have some reservations here regarding the const
constants are compiler optimising flags
they do not decide where an variable should be placed.. i.e. which segment
the variable should reside.
a const auto int i; is stored in stack segment
const static int i; is stored in code segment etc etc
correct me if i am wrong
Banfa 9,065
Expert Mod 8TB
constants are compiler optimising flags
they do not decide where an variable should be placed.. i.e. which segment
Yes it is a flag to help compiler optimisation and one of the optimisations the compiler can make is choosing where the variable should be place, i.e. which segment.
A static const may exist in the code section but like I said on many embedded platforms the static consts are all lumped together in a single text section which is then often left in ROM.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: S. A. Hussain |
last post by:
Where Global variables created in STACK or HEAP in C/C++?
ve##tolimitsyahoocom, delete ##
|
by: hmiller |
last post by:
Hey there folks,
I was wondering if there was a way to store a list of variables in a
table and then call them one at a time in some loop method.
Here's what I've got:
A table "Tab Names"...
|
by: Muffinthief |
last post by:
I'm have been a long time user of python, and have recently picked up C++, but really bothers me is that in c++ you can only store integers up to a certain number (2147483647). How are variables...
|
by: mahendra23 |
last post by:
can you tell me where ststic variables are stored in stack or heap
|
by: Kislay |
last post by:
Which of the following is correct regarding the storage of global
variables :
1. Global variables exist in a memory area that exists from before the
first reference
in a program until after the...
|
by: rashmiharitas |
last post by:
where are statics variables stored?
static variables and static methods can be used without creation of objects
so in which part of memory is static stored
plz help me
|
by: hsegoy1979 |
last post by:
Dear All
Iam new to asp.net. I want to display image on server machine uploaded from client machine using javascript. Any Help
Thankx in Advance
Yogesh
|
by: neovantage |
last post by:
Hey,
Can some one guide me that from where these 2 css warnings come from as i am unable to find those id's in my css file named as
here is the url which gives warnings on my contact page
...
|
by: visweswaran2830 |
last post by:
I want to know where the settings and username are stored in windows XP. Consider, I have created one user account with password protection. But I forgot the password for that user, Now how can I get...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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...
| |