Where are these variables stored exactly?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jithendra
    New Member
    • Jul 2006
    • 2

    #1

    Where are these variables stored exactly?

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

    Thanks in advance.
  • Ashish_CPP
    New Member
    • Jul 2006
    • 35

    #2
    Originally posted by jithendra
    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.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      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.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Copied reply received in PM

        Originally posted by jithendra 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.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          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?

          Comment

          • svlsr2000
            Recognized Expert New Member
            • Feb 2007
            • 181

            #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::
            Code:
            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.
            Last edited by sicarie; Feb 14 '07, 07:42 PM. Reason: Added code tags

            Comment

            • nithinkotekar
              New Member
              • May 2007
              • 1

              #7
              Originally posted by svlsr2000
              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::
              Code:
              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

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by nithinkotekar
                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.

                Comment

                Working...