How is the memory layout of a C/C++ program?

后端 未结 6 2048
感动是毒
感动是毒 2021-01-31 22:00

I know that there are sections like Stack, Heap, Code and Data. Stack/Heap do they use the same section of memory as they can grow independently? What is this code section? When

相关标签:
6条回答
  • 2021-01-31 22:03

    As an addendum to the answers, here is a quote from GotW that classifies some major memory areas (note the difference between free-store, which is what I would usually refer to as the heap, and the actual heap, which is the part managed through malloc/free). The article is a bit old so I don't know if it applies to modern C++; so far I haven't found a direct contradiction.

    Const Data The const data area stores string literals and other data whose values are known at compile time. No objects of class type can exist in this area. All data in this area is available during the entire lifetime of the program. Further, all of this data is read-only, and the results of trying to modify it are undefined. This is in part because even the underlying storage format is subject to arbitrary optimization by the implementation. For example, a particular compiler may store string literals in overlapping objects if it wants to.

    Stack The stack stores automatic variables. Typically allocation is much faster than for dynamic storage (heap or free store) because a memory allocation involves only pointer increment rather than more complex management. Objects are constructed immediately after memory is allocated and destroyed immediately before memory is deallocated, so there is no opportunity for programmers to directly manipulate allocated but uninitialized stack space (barring willful tampering using explicit dtors and placement new).

    Free Store The free store is one of the two dynamic memory areas, allocated/freed by new/delete. Object lifetime can be less than the time the storage is allocated; that is, free store objects can have memory allocated without being immediately initialized, and can be destroyed without the memory being immediately deallocated. During the period when the storage is allocated but outside the object's lifetime, the storage may be accessed and manipulated through a void* but none of the proto-object's nonstatic members or member functions may be accessed, have their addresses taken, or be otherwise manipulated.

    Heap The heap is the other dynamic memory area, allocated/freed by malloc/free and their variants. Note that while the default global new and delete might be implemented in terms of malloc and free by a particular compiler, the heap is not the same as free store and memory allocated in one area cannot be safely deallocated in the other. Memory allocated from the heap can be used for objects of class type by placement-new construction and explicit destruction. If so used, the notes about free store object lifetime apply similarly here.

    Global/Static Global or static variables and objects have their storage allocated at program startup, but may not be initialized until after the program has begun executing. For instance, a static variable in a function is initialized only the first time program execution passes through its definition. The order of initialization of global variables across translation units is not defined, and special care is needed to manage dependencies between global objects (including class statics). As always, uninitialized proto- objects' storage may be accessed and manipulated through a void* but no nonstatic members or member functions may be used or referenced outside the object's actual lifetime.

    0 讨论(0)
  • 2021-01-31 22:05

    (Note: The following applies to Linux)

    The stack and heap of a process both exist in the "same" part of a process's memory. The stack and heap grow towards each other (initially, when the process is started, the stack occupies the entire area that can be occupied by the combination of the stack and the heap; each memory allocation (malloc/free/new/delete) can push the boundary between the stack and the heap either up or down). The BSS section, also located on the same OS-allocated process space, is in its own section and contains global variables. Read-only data resides in the rodata section and contains such things as string literals. For example, if your code has the line:

    char tmpStr[] = "hello";
    

    Then, the portion of the source code containing "hello" will reside in the rodata section.

    A good, thorough book on this is Randall E. Bryant's Computer Systems.

    0 讨论(0)
  • 2021-01-31 22:08

    There's very little that's actually definitive about C++ memory layouts. However, most modern OS's use a somewhat similar system, and the segments are separated based on permissions.

    Code has execute permission. The other segments don't. In a Windows application, you can't just put some native code on the stack and execute. Linux offers the same functionality- it's in the x86 architecture.

    Data is data that's part of the result (.exe, etc) but can't be written to. This section is basically where literals go. Only read permission in this section.

    Those two segments are part of the resulting file. Stack and Heap are runtime allocated, instead of mapped off the hard drive.

    Stack is essentially one, large (1MB or so, many compilers offer a setting for it) heap allocation. The compiler manages it for you.

    Heap memory is memory that the OS returns to you through some process. Normally, heap is a heap (the data structure) of pointers to free memory blocks and their sizes. When you request one, it's given to you. Both read and write permissions here, but no execute.

    There is read-only memory(ROM). However, this is just the Data section. You can't alter it at runtime. When you make a const variable, nothing special happens to it in memory. All that happens is that the compiler will only create certain instructions on it. That's it. x86 has no knowledge or notion of const- it's all in the compiler.

    0 讨论(0)
  • 2021-01-31 22:13

    I was in same dilemma when I was reading about memory layout's of C/C++. Here is the link which I followed to get the questions cleared.

    http://www.geeksforgeeks.org/memory-layout-of-c-program/

    The link's main illustration is added here:

    I hope this helps 'the one' finding answers to similar question.

    0 讨论(0)
  • 2021-01-31 22:22

    It's mostly OS dependant.

    For windows take a look at Petzold or Richter's books. For Linux you can read the source !

    0 讨论(0)
  • 2021-01-31 22:25

    AFAIK:

    Stack/Heap do they use the same section of memory as they can grow independently?

    They can grow indipendently.

    What is this code section?

    A read-only segment where code and const data are stored.

    When I have a function is it a part of the stack or the code section?

    The definition (code) of the function will be in the CS. The arguments of each call are passed on the stack.

    Also what is this initialized/uninitialized data segment?

    The data segment is where globals/static variables are stored.

    Are there read only memory section available?

    The code segment. I suppose some OS's might offer primitives for creating custom read-only segments.

    When I have a const variable, what is actually happening is it that the compiler marks a memory section as read only or does it put into a read only memory section.

    It goes into the CS.

    Where are static data kept? Where are global data kept?

    The data segment.

    0 讨论(0)
提交回复
热议问题