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

Where are these variables stored exactly?

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

Thanks in advance.
Jul 13 '06 #1
Share this Question
Share on Google+
7 Replies


P: 35
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
Expert Mod 5K+
P: 8,916
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
Expert Mod 5K+
P: 8,916
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
Expert Mod 5K+
P: 8,916
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

Expert 100+
P: 181
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

P: 1
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
Expert Mod 5K+
P: 8,916
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

Post your reply

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