Today we will learn about the memory distribution in C language, as well as the contents and characteristics of these partitions. The mind map for today is as follows.

The C language, as a programming language that directly interacts with hardware at a low level, is used to write programs for many embedded devices in reality. The core feature of C language is its ability to manipulate memory, allowing programs to run on hardware with very limited memory, significantly reducing production costs.
The C language divides memory into five areas: the code segment, constant segment, global/static segment, heap segment, and stack segment.
Code Segment
The code segment is responsible for storing the binary code files that the computer can execute. To prevent issues during program execution, the contents of the code segment are read-only and cannot be modified; additionally, the size of the code is determined before the program runs.
Constant Segment
This segment stores constants such as strings and numbers. This content is also unmodifiable during program execution.
Global/Static Segment
This memory is used to store global and static variables, which exist throughout the entire lifecycle of the program and are allocated by the system at startup and released at termination. The global/static segment is divided into two parts: initialized and uninitialized data segments.
Initialized Data Segment (.data)
This segment stores explicitly initialized global and static variables.
Uninitialized Data Segment (.bss)
This segment stores uninitialized global and static variables, with a default value of 0.
Heap Segment
The memory dynamically allocated by the programmer (such as space requested by malloc() and calloc()) falls into this segment. The memory addresses in the heap segment grow from low to high, and the allocation and deallocation of this memory is determined and implemented by the programmer.
It is important to note that while the heap segment has a large space, the allocation is not contiguous, and the access speed is relatively slow;
it follows the First In First Out (FIFO) principle.
Stack Segment
The stack segment is used to store local variables and function contexts (return addresses, parameters, etc.), with memory allocation and deallocation managed by the system.
The addresses in the stack segment grow from high to low, with its maximum size determined at compile time, offering fast access and contiguous memory allocation, but with limited size.
Since the contents stored in the stack segment only exist within the function’s scope, they are destroyed once the function execution is complete.
The stack segment follows the Last In First Out (LIFO) principle, meaning that the data that enters last is released first. For example, if a local variable is defined first in a function, it will be released after the function execution is complete, ensuring that nothing within the function is still calling it.
Operations on the stack are referred to as pushing (Push) and popping (Pop).
Summary
The code segment is used solely for storing code;
the constant segment stores strings and numbers;
the global/static segment is divided into initialized (.data) and uninitialized (.bss) to store global and static variables;
the heap segment is for dynamically allocated memory, following FIFO;
the stack segment stores local variables and function contexts, following LIFO.
The heap and stack segments are not adjacent, with a segment of memory in between to prevent data overlap.
Stack operations only require moving the stack pointer, making them highly efficient; heap operations require finding suitable memory blocks, which is less efficient.