The memory in the computer is byte-addressed
The storage unit of each address can store one byte (8 bits) of data
CPU gets instructions and data through memory address
I don’t care about where and how the space represented by this address is located,
Because the hardware design guarantees that an address corresponds to a fixed space.
That is: the memory address and the space pointed to by the address together form a memory unit
-----------------------------------
For programs written in C language, the memory is mainly divided into 5 parts
(1) Stack and (2) Heap is the space allocated by the system at runtime
(3) BSS segment (uninitialized), (4) data segment (initialized), (5) code segment is allocated by the compiler
==============================
The BSS section, data section, and code section are allocated by the compiler and loaded when the program starts
Since the uninitialized global is stored in the BSS segment,
The initialized global variables are stored in the data segment
Programs should use as few global variables as possible to save program compilation and startup time
Global variables will always exist as long as the process exists, always occupying memory
Global variables will only be loaded when the program is started, so global variables cannot be too many
Too much will increase startup time and increase compilation time (packaging time becomes longer)
===============
Detailed explanation:
===================================
Static storage area
Read-only data area, read-write data area, uninitialized area (BSS)
All are determined during the program compilation and connection stage, and will not change during the program execution stage
=============================
Dynamic storage area
Divided into heap and stack
All are dynamically allocated during execution, and the size changes dynamically accordingly
From the perspective of the implementation of memory management, the heap uses a linked list, while the stack uses a linear storage method
===========================
1. Stack: System allocates space
==============================
First-in-last-out principle (Pistol bullets)
By constantly moving the pointer on the top of the stack, the bullet is loaded and launched
Secondly, the stack is used as a storage structure in memory, which usually stores temporarily created local variables of the program
That is, the variables defined in the function braces {}, which also include the formal parameters of the function call, and the return value after the call
The stack is a data structure extended from high address to address
Finally, the stack also has the characteristics of "small memory, automation, and possible overflow"
The address at the top of the stack and the maximum capacity of the stack are generally pre-defined by the system, usually not too large
Because the stack stores local variables, and the memory space occupied by local variables is its
When the code segment or function segment ends, it will be recycled and reused by the system
Therefore, the stack space is automatically managed by recycling and generally does not need to be considered for operation
If the space applied for a local variable exceeds the remaining space of the stack, it may appear
"Stack overflow", generally do not apply for too much space in the stack, such as a very large array
Recursively call functions that are repeated many times
2. Heap: allocated and released by the programmer
==========================
If the programmer does not release it, the OS will reclaim it when the program ends.
If the programmer does not release the heap memory in time after using the application,
Then this memory is lost. (The process thinks that the memory is not being used, but
The memory still belongs to this process in the heap memory record, so when you need to allocate space
Will reapply for new memory (2 is not to reuse this memory)
Is what we often call memory leaks
The allocation of memory is not continuous, similar to a linked list.
Heap: Heap, free application space, grows from low to high memory address,
Its size is determined by the upper limit of system memory/virtual memory, which is slower, but has greater freedom and large available space.
Each thread will have its own stack, but the heap space is shared.
(1) Usually store the storage space dynamically allocated during program operation;
(2) The heap is a data structure that extends from low addresses to high addresses, and is a discontinuous memory area;
(3) Use malloc and new to allocate memory in the heap
3.BSS段 block started by symbol
=============================
Usually refers to the storage of uninitialized global variables and static variables in the program
Static allocation is usually cleared at the beginning of the program.
Static variables: modified with static
4. Data segment
=======
Usually refers to the global variables and static variables and string constants that have been initialized in the program
5. Code snippet
=======
Usually refers to a memory area used to store program execution code
The size of this area has been determined before the program runs.
=========================================
int a = 0; //Global initialization area
char *p1; //Global uninitialized area
void main()
{
int b; //stack
char s[] = "abc";//stack
char *p2; //stack
char *p3 = "123456"; //123456\0 is in the constant area and p3 is on the stack; experience the difference with char s[]="abc";
static int c =0; //Global initialization area
p2 = (char *)malloc(20); //heap area
strcpy(p1, “123456”); //123456\0 is in the constant area, the compiler may optimize it with the “123456” pointed to by p3
}
=====================================
over