473,240 Members | 1,954 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,240 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 43057
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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...

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.