Reference some books, welcome to correct, thank you.

5 floors space layout

  1. Code snippets: Stores program execution code, which may contain read-only constant variables (such as string constants). A program can have multiple copies in memory.
  2. Initialize data segment: a static memory allocation in an executable file. Initialized global variables, static variables.
  3. Uninitialized data segment: uninitialized global variables, static variables.
  4. Heap: Stores memory segments that are dynamically allocated while a process is running
  5. Stack: Also called stack, holds local variables. When a function is called, the stack is used to pass parameters and return values, which can be easily used to save or restore the call scene.

Int a = 0; // global initializer char *p1; // Global uninitialized areamain() { int b; // stack char s[] ="abc"; / / stack char * (p2); // stack char *p3 ="123456"; // 123456\0 in constant area, p3 in stack. Static int c = 0; // global (static) initialization area // 👇 allocates 10 - and 20-byte areas in the heap area. p1 = (char *)malloc(10); // 🆘️p1 p2 itself is in the stack. p2 = (char *)malloc(20); // 123456\0 is placed in the constant area, and the compiler may refer to it as p3"123456"Optimized into one place. strcpy(p1,"123456");    
}
Copy the code

You can roughly view the entire program in memory allocation, there are the following two features:

  1. Incoming parameters, local variables, are distributed at the top of the stack and grow down as the number of subfunctions increases.
  2. Function calling addresses (function running code), global variables, and static variables all exist at the bottom of allocated memory, and the malloc allocated heap exists on top of this memory and grows upwards.

Thus, we can summarize in detail:

The difference between heap and stack

The difference between Stack (to a restaurant) Pile (cook your own food)
Different growth patterns Grow down Grow up
Different space sizes Stack space is limited. Stack top preset, if the application space exceeds the free space will overflow. Ulimit -a can see the stack size limit The heap can be as large as 4G. Is a discontinuous memory region. List traversal direction is low to high address. Heap size depends on the computer systemEffective virtual memorylimit
Different management methods Stacks are allocated automatically by the compiler without programmer control The heap is controlled by the programmer himself
The allocation efficiency is different, the heap is much less efficient than the stack. The stack is a data structure provided by the machine system. The bottom layer of the computer provides support for the stack: special registers are allocated to store the address of the stack, and special instructions are given to push and unload the stack. Heap is provided by the C/C + + libraries, library functions will be according to certain algorithm search available in the heap memory enough space, the size of size if there is not enough space (which may be due to too much memory fragments), it is possible to call system function to increase the application data segment of memory space, thus have the opportunity to get enough memory size, and then to return.
Whether it will fragment Stacks do not generate fragmentation because stacks are first in and last out The heap is prone to fragmentation, and multiple new/ DELETES will cause memory discontinuity, resulting in a large amount of fragmentation.
Different ways of distribution Stacks can be statically allocated or dynamically allocated. Stack allocation is different from heap allocation, which is released by the compiler without manual implementation. The heap is allocated dynamically. The programmer applies for it himself and specifies the size
The system responds after the application is made As long as the remaining space of the stack is larger than the requested space, the system will provide memory for the program; Otherwise, an exception is reported, indicating stack overflow. Iterates through the linked list of free memory addresses, looking for the heap node that has the first space > requested space. Delete this node and assign it to the program. (The size of the allocated space is recorded at the beginning of the address — convenient for delete.) The system automatically puts the excess back into the free linked list.
Store content When A function is called, it is pushed in the following order: the address of the next executable statement of A function call statement → the parameters of B function (pushed from right to left) → local variables in C function. (Static variables are not pushed) The function call ends, the reverse order is removed from the stack, and finally the top of the stack pointer points to the next instruction of the main function, from which the program continues to run. The size of the heap is stored in a Byte in the header, and the contents of the heap are determined by the programmer.

Question:

Is virtual memory capacity limited by ()?
A. Disk space B. Physical memory size
C. Actual address where data is stored D. Computer address bits

A b C D

Virtual memory is the creation of a space on the disk to alleviate the physical memory shortage. The maximum memory a computer can support is determined by the number of address bits on the computer, which is the maximum addressing power of the computer. In addition, the size of virtual memory is less than or equal to the sum of memory capacity and external memory capacity.

Is the capacity of virtual storage most affected by ()?
A. Disk space B. Physical memory size
C. Actual address where data is stored D. Computer address bits

B