473,396 Members | 1,935 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,396 software developers and data experts.

Where are these variables stored exactly?

Dear all,
Where are global , static , and automatic variables stored in memory?

Thanks in advance.
Jul 13 '06 #1
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.
Jul 13 '06 #2
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.
Jul 13 '06 #3
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.
Jul 14 '06 #4
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?
Jul 14 '06 #5
svlsr2000
181 Expert 100+
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::
Expand|Select|Wrap|Line Numbers
  1. void MyFunction(int a)
  2. {
  3.         static int x = 0;
  4.         printf("my number: ");
  5.         printf("%d, %d\n", a, x);
  6. when compiled and disassembled in windows environment
  7. BSS       SEGMENT
  8. ?x@?1??MyFunction@@9@9 DD 01H DUP (?)           ; `MyFunction'::`2'::x
  9. _BSS    ENDS
  10. _DATA   SEGMENT
  11. $SG796  DB      'my number: ', 00H
  12. $SG797  DB      '%d, %d', 0aH, 00H
  13. _DATA   ENDS
  14. PUBLIC  _MyFunction
  15. EXTRN   _printf:NEAR
  16. ; Function compile flags: /Odt
  17. _TEXT   SEGMENT
  18. _a$ = 8                                 ; size = 4
  19. _MyFunction PROC NEAR
  20. ; Line 4
  21.         push    ebp
  22.         mov     ebp, esp
  23. ; Line 6
  24.         push    OFFSET FLAT:$SG796
  25.         call    _printf
  26.         add     esp, 4
  27. ; Line 7
  28.         mov     eax, DWORD PTR ?x@?1??MyFunction@@9@9
  29.         push    eax
  30.         mov     ecx, DWORD PTR _a$[ebp]
  31.         push    ecx
  32.         push    OFFSET FLAT:$SG797
  33.         call    _printf
  34.         add     esp, 12                                 ; 0000000cH
  35. ; Line 8
  36.         pop     ebp
  37.         ret     0
  38. _MyFunction ENDP
  39. _TEXT   ENDS
  40.  
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.
Feb 14 '07 #6
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::
Expand|Select|Wrap|Line Numbers
  1. void MyFunction(int a)
  2. {
  3.         static int x = 0;
  4.         printf("my number: ");
  5.         printf("%d, %d\n", a, x);
  6. when compiled and disassembled in windows environment
  7. BSS       SEGMENT
  8. ?x@?1??MyFunction@@9@9 DD 01H DUP (?)           ; `MyFunction'::`2'::x
  9. _BSS    ENDS
  10. _DATA   SEGMENT
  11. $SG796  DB      'my number: ', 00H
  12. $SG797  DB      '%d, %d', 0aH, 00H
  13. _DATA   ENDS
  14. PUBLIC  _MyFunction
  15. EXTRN   _printf:NEAR
  16. ; Function compile flags: /Odt
  17. _TEXT   SEGMENT
  18. _a$ = 8                                 ; size = 4
  19. _MyFunction PROC NEAR
  20. ; Line 4
  21.         push    ebp
  22.         mov     ebp, esp
  23. ; Line 6
  24.         push    OFFSET FLAT:$SG796
  25.         call    _printf
  26.         add     esp, 4
  27. ; Line 7
  28.         mov     eax, DWORD PTR ?x@?1??MyFunction@@9@9
  29.         push    eax
  30.         mov     ecx, DWORD PTR _a$[ebp]
  31.         push    ecx
  32.         push    OFFSET FLAT:$SG797
  33.         call    _printf
  34.         add     esp, 12                                 ; 0000000cH
  35. ; Line 8
  36.         pop     ebp
  37.         ret     0
  38. _MyFunction ENDP
  39. _TEXT   ENDS
  40.  
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
May 18 '07 #7
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.
May 18 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: S. A. Hussain | last post by:
Where Global variables created in STACK or HEAP in C/C++? ve##tolimitsyahoocom, delete ##
4
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"...
5
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...
2
by: mahendra23 | last post by:
can you tell me where ststic variables are stored in stack or heap
43
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...
8
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
6
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
3
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 ...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
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...

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.