How memory space and variables are located in C++?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vnpatriot7
    New Member
    • Dec 2007
    • 2

    How memory space and variables are located in C++?

    Hi everybody,
    I have two questions:
    1) About memory space in C++
    2) About global and static variable

    As what I read somewhere that after compilation process which translate C++ code into machine language, your application is given a certain amount of memory to use, that memory space is divided into 4 segments as follow

    a. Code segment, where all application code is stored
    b. Data segment, where global data is stored
    c. Stack segment, where local variables are stored
    d. Heap segment, where dynamic memory is used

    First question, is my statement correct?

    1. If correct, the second question:
      Are all global and static variables initialized when program start and exist until the program is terminated? Where are these types of variable located? Is it data segment?

    2. If the first question is wrong, my second question
      How is memory space allocated for an application? Where are all types of variable located?

    Anyway, thanks a lot for your concern and help
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Read this: http://www.thescripts.com/forum/thread737451.html.

    Then post again and I'll go from there.

    Comment

    • zephyrus360
      New Member
      • Apr 2008
      • 5

      #3
      Coincidently, i have exactly the same question and doubts about location of static and global members in data segment.

      Can someone plz explain ?? The original post hasnt been answered yet.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Data can be located in 1 of 3 areas

        The Data Segment
        The Heap
        The Stack

        Depending on the platform the heap and stack are sometimes actually located in the data segment, in fact the term data segment is used ambiguously and differently depending on the platform. Sometimes in means the segment used to store variables not on the stack and the heap and sometimes it means all of ram available to the application including the heap and stack.

        Anyway for the rest of this post I will treat the term data segment as meaning a completely separate segment to the heap and stack.

        I will start by describing what goes on the heap and stack.

        The stack contains (among other things) all variables with storage class auto. A variable with storage class auto is one that is declared inside a function without the static keyword, that is a variable that only exists for the lifetime of the function. Additionally the stack normally contains most of the parameters in a function call, however on some platforms parameters over a certain number or size sometimes have memory allocated from the heap with pointers stored on the stack. The stack also contains stack frames, this the the data that is required for the processor to know how to return from a function and leave the processor in the correct state to continue. Again what exactly is in a stack frame is platform dependent.

        The heap contains variables that have had memory allocated for them either with an explicit call to malloc or new or automatically by the compiler. The memory for these variables has to be explicitly released by whoever allocated it when it is no longer required.

        The data segment basically contains everything else, that is all global variables (declared with or without static). These are basically split into 2 groups uninitialised variables, that is variables that have not been given an explicit starting value in the code, these are initialised to 0. Initialised variables, these are variables that have been given an explicit value in the code, in the case of simple types (not classes) the program will contain a block that is the initialisation values of these variables and this is copied to the variables before main is called so that they have the correct starting values.

        There is 1 more class of data, string (and stricture) constants and const variables, numeric constants are normally compiled straight into hardcoded values in the machine code. Where these reside is platform dependent. On some platforms (PC for instance) they appear in the data segment (and are possibly alterable but you should not change them) on other platforms (embedded for instance) they appear in the same storage medium as the program (FLASH memory) and are therefore prevent from being altered by hardware constraints.


        The overriding thing you should take from this is the number of times I have said "it's platform dependent", C and C++ make no real constraints on where or how a platform stores it's variables. If you really need to know read you platform documentation however most of the code you write should not need to care about these details.

        Oh and read the linked article on the dangers of global variables.

        Comment

        • zephyrus360
          New Member
          • Apr 2008
          • 5

          #5
          Hey, thanks for that info !!

          Also found the following link helpful..

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by zephyrus360
            Also found the following link helpful..
            http://www2.cs.uregina.ca/~hamilton/...emmngment.html
            Yes but those things are strictly speaking the responsibility of the OS or in some cases the processor and nothing to do with either C or C++ (except of course C certainly and C++ in a few cases are used to write OS software).

            Comment

            Working...