1. What is memory allocation and deallocation?
When a program runs, it needs memory to store variables, objects, function call states, and dynamically created data structures.
Memory allocation: a program requests usable memory from the operating system, runtime, or memory allocator.
Memory deallocation: the program or runtime returns memory that is no longer in use to the memory management system so that it can be reused.
Memory allocation and deallocation run throughout the entire lifecycle of a program.
For example, in C:
#include <stdlib.h>
int main() {
int *ptr = malloc(sizeof(int));
if (ptr == NULL) {
return 1;
}
*ptr = 100;
free(ptr);
ptr = NULL;
return 0;
}
Execution process:
flowchart TB
A["Program requests memory"] --> B["Allocator finds available memory"]
B --> C["Return memory address"]
C --> D["Program uses memory"]
D --> E["Program no longer needs memory"]
E --> F["Call free to release memory"]
F --> G["Memory returned to allocator"]
G --> H["Can be reused by later allocations"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style C fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
style E fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style F fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
Three concepts need to be distinguished:
- Requesting memory: obtain a region of memory that can be used.
- Object creation: create an object with a specific type and lifetime in memory.
- Memory release: end the use of the relevant storage and return it to the management system.
In C++, memory allocation and object construction are two different operations; in managed languages such as Java and Python, memory management is mainly handled by the runtime.
2. Main memory areas of a program
To understand memory allocation, you first need to understand the common memory areas when a program runs.
The virtual address space of a typical process can be abstracted into the following parts:
| Memory Area | Main Purpose | Lifetime or Management Method |
|---|---|---|
| Text Segment | Stores executable instructions | Usually exists for the lifetime of the process |
| Data Segment | Stores initialized global and static variables | Usually exists for the lifetime of the process |
| BSS Segment | Stores static storage duration variables that are not explicitly initialized or are zero-initialized | Usually exists for the lifetime of the process |
| Heap | Supports dynamic memory allocation | Varies according to the allocator and program management strategy |
| Stack | Stores function call frames, local variables, etc. | Usually changes with function calls and returns |
| Memory-mapped region | Maps files, shared memory, anonymous memory, etc. | Managed by mapping and unmapping operations |
These areas are a common implementation model; they do not mean that all programming languages, operating systems, and processors use exactly the same layout.
2.1 Stack memory allocation
The stack is usually used to manage execution state during function calls.
void calculate() {
int a = 10;
int b = 20;
int result = a + b;
}
In common implementations, calling calculate() creates a stack frame to store local data, the return address, and necessary register state.
After the function returns, the corresponding stack frame is no longer valid, and the space it occupies can be reused by subsequent function calls.
Typical characteristics:
- Allocation and deallocation are usually managed automatically by the function call mechanism.
- Lifetime is usually related to scope or function calls.
- Allocation and reclamation costs are relatively low.
- Space is usually relatively limited.
- Not suitable for data that grows indefinitely or whose lifetime needs to extend beyond the current call.
Note: It cannot simply be assumed that all local variables are necessarily stored on the stack. The compiler may place variables in registers, eliminate variables, or change their storage location due to factors such as escape analysis.
2.2 Heap memory allocation
The heap is used to support dynamic data whose lifetime does not depend entirely on the current function call.
```c #includeTypical characteristics of heap memory:
- Allocated dynamically through an allocator.
- The allocation size can be decided at runtime.
- The lifetime can span function calls.
- Requires explicit release or management by a garbage collector.
- Allocation and reclamation involve extra management overhead.
Heap memory does not necessarily mean that the operating system allocates it one piece at a time. Most runtimes use a memory allocator to manage memory blocks they have already obtained, in order to reduce the overhead of system calls.
2.3 Differences Between the Stack and the Heap
| Dimension | Stack | Heap |
|---|---|---|
| Managed by | Usually managed jointly by the compiler and the calling convention | Usually managed by a memory allocator or the runtime |
| Allocation method | Commonly done by adjusting the stack pointer | Usually requires searching for or organizing available memory blocks |
| Lifetime | Usually tied to the call frame | Can be managed independently by the program or the runtime |
| Allocation speed | Usually faster | Usually has more management overhead |
| Space size | Usually relatively limited | Usually larger, but limited by the process address space and system resources |
| Typical problems | Stack overflow, the lifetime of stack objects ending | Memory leaks, fragmentation, double free |
The two are not simply "the stack is fast and the heap is slow." Actual performance depends on the allocator, cache locality, object size, allocation frequency, and memory access patterns.
3. How Memory Allocation Works
When a program calls malloc, new, or another allocation interface, it does not necessarily request a new piece of physical memory directly from the operating system.
Usually there is a memory allocator in between.
3.1 Memory Allocators
A memory allocator is responsible for managing available memory blocks and providing allocation and deallocation interfaces to programs.
Common responsibilities include:
- Managing free memory blocks.
- Selecting a suitable memory block based on the requested size.
- Maintaining metadata for memory blocks.
- Handling memory alignment requirements.
- Merging adjacent free blocks.
- Requesting memory from or returning memory to the operating system when necessary.
Typical allocation process:
flowchart TB
A["Program requests memory of the specified size"] --> B["Enter the memory allocator"]
B --> C["Search for a free memory block"]
C --> D{"Is a suitable memory block available?"}
D -->|"Yes"| E["Select and split the memory block"]
D -->|"No"| F["Request more memory from the operating system"]
F --> G["Manage the newly added memory region"]
G --> E
E --> H["Update allocator metadata"]
H --> I["Return a usable address"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style C fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style F fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style H fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style I fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
3.2 Requesting Memory from the Operating System
On operating systems such as Linux, user-space allocators can obtain larger memory regions through system calls or related interfaces.
Common mechanisms include:
brk/sbrk: Adjusts the end of the process data segment; this is a traditional heap management mechanism.mmap: Establishes a file or anonymous memory mapping.munmap: Removes a memory mapping.
Modern allocators usually choose different paths based on the requested size, allocation strategy, and runtime environment.
For example:
- Small objects may be allocated from an existing memory pool or free block.
- Large objects may obtain a separate region directly through memory mapping.
- After a small object is freed, the memory may remain in the allocator waiting to be reused.
- When a large mapped region is freed, the allocator may unmap it and return the address space.
Therefore, after calling free(), the memory occupied by the process does not necessarily decrease immediately.
3.3 Virtual Memory and Physical Memory
Modern operating systems usually use virtual memory mechanisms.
The addresses used by programs are generally virtual addresses, and the operating system and processor map virtual addresses to physical memory through mechanisms such as page tables.
It is necessary to distinguish:
- Virtual address space: the address space a process sees.
- Physical memory: the actual RAM.
- Memory mapping: the mapping relationship between virtual addresses and physical resources.
- Resident memory: the pages that are currently actually resident in physical memory.
Successfully requesting memory does not always mean that the corresponding physical pages have all been allocated and are resident. The specific behavior depends on the operating system's memory commit policy, page access patterns, and resource limits.
4. Memory Allocation and Deallocation in C
C provides a set of standard dynamic memory management functions, mainly defined in <stdlib.h>.
4.1 malloc: Allocate Memory of a Specified Size
malloc is used to request a specified number of bytes of memory.
#include <stdlib.h>
int *ptr = malloc(sizeof(int));
if (ptr == NULL) {
// allocation failed
return 1;
}
*ptr = 100;
free(ptr);
Function prototype:
void *malloc(size_t size);
Characteristics:
- The parameter specifies the number of bytes to request.
- On success, it returns the starting address of the allocated memory.
- On failure, it returns a null pointer.
- The contents of the returned memory are not guaranteed to be initialized to zero.
- The returned memory has a basic alignment guarantee suitable for ordinary objects; the specific guarantee is specified by the C standard.
If you need to allocate an array, be careful to avoid integer overflow when calculating the total number of bytes.
4.2 calloc: Allocate and Zero
calloc is used to allocate a specified number of elements of a specified size and initialize all bytes in the allocated region to zero.
#include <stdlib.h>
int *arr = calloc(5, sizeof(int));
if (arr == NULL) {
return 1;
}
free(arr);
Function prototype:
void *calloc(size_t count, size_t size);
Main differences from malloc:
| Function | Parameters | Initialization behavior |
|---|---|---|
malloc |
Total number of bytes | No initialization |
calloc |
Number of elements, size of a single element | Sets the bytes of the allocated region to zero |
Note: Setting memory bytes to zero is not equivalent to performing language-level default initialization for all possible C types. For common integer types, zero bytes represent integer zero; however, this cannot be generalized unconditionally to all types and object representations.
4.3 realloc: Resize Memory
realloc is used to resize a dynamically allocated memory block.
#include <stdlib.h>
int *arr = malloc(3 * sizeof(int));
if (arr == NULL) {
return 1;
}
int *new_arr = realloc(arr, 5 * sizeof(int));
if (new_arr == NULL) {
free(arr);
return 1;
}
arr = new_arr;
free(arr);
realloc may:
- Extend the memory block in place.
- Allocate new memory at another location and copy the original contents.
- Free the old memory block.
- Return a null pointer on failure, while preserving the original memory block.
Therefore, you must not directly use the original pointer to receive the return value of realloc:
arr = realloc(arr, new_size); // not recommended
If the allocation fails, the original pointer may be lost, making the original memory impossible to free.
A safer approach is to use a temporary pointer.
int *temp = realloc(arr, new_size);
if (temp != NULL) {
arr = temp;
}
For realloc(ptr, 0), there are differences between C standard versions and implementations that require special attention. In real code, it is advisable to handle the zero-length case explicitly rather than relying on its behavior.
4.4 free: Free Dynamic Memory
free is used to free a memory block obtained from a compatible dynamic memory allocation function.
int *ptr = malloc(sizeof(int));
if (ptr != NULL) {
*ptr = 100;
free(ptr);
ptr = NULL;
}
The following rules must be observed:
- Only valid dynamically allocated pointers may be freed.
- The address of an ordinary local variable must not be freed.
- The same block of memory must not be freed twice.
- The memory must not be accessed after it has been freed.
free(NULL)is a legal operation and performs no deallocation.
4.5 Complete Example of Dynamic Memory Management in C
#include <stdio.h>
#include <stdlib.h>
int main(void) {
size_t count = 5;
int *arr = malloc(count * sizeof(*arr));
if (arr == NULL) {
return 1;
}
for (size_t i = 0; i < count; i++) {
arr[i] = (int)(i + 1) * 10;
}
int *temp = realloc(arr, 10 * sizeof(*arr));
if (temp == NULL) {
free(arr);
return 1;
}
arr = temp;
for (size_t i = count; i < 10; i++) {
arr[i] = 0;
}
for (size_t i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
free(arr);
arr = NULL;
return 0;
}
This example illustrates the basic principles of dynamic memory management:
- Check the result after allocating memory.
- Ensure allocation succeeds before using the memory.
- Use a temporary pointer when resizing memory.
- Do not access the original address after freeing the memory.
5. Memory Allocation and Deallocation in C++
C++ builds on C by providing new and delete, and further introduces mechanisms such as object construction, destruction, allocation functions, and smart pointers.
5.1 new: Allocate Memory and Construct an Object
int* ptr = new int(100);
A new expression usually consists of two phases:
- Obtain sufficient storage space.
- Construct an object in that storage space.
For class objects, the constructor is called.
#include <iostream>
class User {
public:
User() {
std::cout << "构造函数执行" << std::endl;
}
~User() {
std::cout << "析构函数执行" << std::endl;
}
};
int main() {
User* user = new User();
delete user;
return 0;
}
5.2 delete: Destroy the Object and Release Memory
A delete expression usually involves:
- Calling the object's destructor.
- Calling the corresponding deallocation function to return the storage space.
int* ptr = new int(100);
delete ptr;
ptr = nullptr;
For arrays, delete[] must be used:
int* arr = new int[5];
delete[] arr;
arr = nullptr;
new[] and delete[] must be correctly paired. Mixing array-form allocation with ordinary delete results in undefined behavior.
5.3 Differences Between malloc/free and new/delete
| Comparison Dimension | malloc/free | new/delete |
|---|---|---|
| Language mechanism | C standard library interface | C++ language expression |
| Memory allocation | Allocates a specified number of bytes | Allocates storage based on the type |
| Object construction | Does not call constructors | Calls constructors |
| Object destruction | Does not call destructors | Calls destructors |
| Return type | void* |
Pointer to the corresponding object type |
| Allocation failure | Usually returns a null pointer | Throws std::bad_alloc by default |
| Array support | By calculating the total number of bytes | new[] / delete[] |
They cannot be arbitrarily cross-paired:
int* ptr = new int(10);
free(ptr); // Error: allocation and deallocation methods do not match
The correct way:
int* ptr = new int(10);
delete ptr;
5.4 placement new: Construct an Object in Specified Memory
C++ also supports placement new, which allows an object to be constructed in already obtained storage space.
#include <new>
#include <cstdlib>
struct User {
int id;
User(int value) : id(value) {}
};
int main() {
void* memory = std::malloc(sizeof(User));
if (memory == nullptr) {
return 1;
}
User* user = new (memory) User(100);
user->~User();
std::free(memory);
return 0;
}
Here:
malloconly allocates raw storage space.- placement new constructs the
Userobject in that space. - Explicitly call the destructor to end the object's lifetime.
freereleases the raw storage space.
This shows that in C++, object lifetime and storage lifetime are two concepts that need to be understood separately.
In practical use, alignment requirements must also be met, and object lifetime management must be ensured to be correct.
6. Modern C++: Smart Pointers and Automatic Resource Management
Manually using new and delete is prone to memory leaks, double frees, and exception safety issues.
Modern C++ recommends using RAII (Resource Acquisition Is Initialization) and smart pointers to manage dynamic resources.
6.1 RAII Mechanism
RAII is a resource management idea:
- Acquire resources when an object is constructed.
- Release resources when the object is destroyed.
- Use object lifetime to manage resource lifetime.
Resources include not only memory, but can also include file handles, locks, network connections, etc.
#include <fstream>
void writeFile() {
std::ofstream file("data.txt");
file << "Hello";
}
When the file object goes out of scope, its destructor is responsible for closing the file.
6.2 unique_ptr: Exclusive Ownership
std::unique_ptr represents exclusive management of a dynamic object.
#include <memory>
int main() {
auto ptr = std::make_unique<int>(100);
return 0;
}
When the lifetime of ptr ends, the object it manages is automatically released.
Exclusive ownership means the same resource cannot be owned simultaneously by multiple ordinary unique_ptr instances.
auto a = std::make_unique<int>(100);
// auto b = a; // Error: cannot copy unique_ptr
auto b = std::move(a);
6.3 shared_ptr: Shared Ownership
std::shared_ptr manages shared objects through reference counting.
#include <memory>
int main() {
auto a = std::make_shared<int>(100);
{
auto b = a;
}
return 0;
}
The object is destroyed only when the last shared owner releases the resource.
The basic process is:
flowchart TB
A["Create shared_ptr"] --> B["Reference count initialized to 1"]
B --> C["Copy shared_ptr"]
C --> D["Reference count increases"]
D --> E["A shared_ptr goes out of scope"]
E --> F["Reference count decreases"]
F --> G{"Is the reference count 0?"}
G -->|"No"| H["Object continues to exist"]
G -->|"Yes"| I["Destroy the object and release resources"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
style E fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style F fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style G fill:#FFF5DE,stroke:#E8D29A,color:#334155
style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style I fill:#E5F4EC,stroke:#A3CBB5,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
6.4 weak_ptr: Weak Reference
std::weak_ptr is used to observe an object managed by shared_ptr without increasing its strong reference count.
A common use is to break circular references.
For example, two objects hold each other via shared_ptr:
struct B;
struct A {
std::shared_ptr<B> b;
};
struct B {
std::shared_ptr<A> a;
};
If two objects hold each other, their reference counts may fail to reach zero even after external owners have released them, preventing resources from being freed.
One of the directions can be changed to weak_ptr:
struct B;
struct A {
std::shared_ptr<B> b;
};
struct B {
std::weak_ptr<A> a;
};
Observing whether an object still exists through a weak reference can avoid resource leaks caused by circular references.
6.5 Comparison of the Three Smart Pointers
| Smart Pointer | Ownership Model | Increases Strong Reference Count | Typical Use |
|---|---|---|---|
unique_ptr |
Exclusive ownership | Not applicable | Single object owner |
shared_ptr |
Shared ownership | Yes | Multiple parties jointly hold an object |
weak_ptr |
Non-owning observation | No | Observe shared objects, break circular references |
Smart pointers can reduce errors from manual resource management, but they do not mean there is no cost at all. shared_ptr in particular must maintain a control block and reference counts, so it should not be overused when shared ownership is not required.
7. Garbage Collection Mechanism
C and C++ usually rely on programmers or resource-management abstractions to explicitly manage dynamic memory. Languages such as Java, Go, and Python mainly use automatic memory management mechanisms.
Among these, garbage collection (GC) is a mechanism that automatically identifies and reclaims unreachable objects.
7.1 What Is a Garbage Object?
A garbage object generally refers to an object that can no longer be accessed from any valid reference entry point in the program.
For example:
User user = new User();
user = null;
If no other valid references point to the object, it may become a candidate for garbage collection.
Note: An object becoming unreachable does not mean it will be reclaimed immediately. The timing of reclamation is determined by the runtime.
7.2 Reachability Analysis
Many modern garbage collectors use reachability analysis.
Its core idea is to start from a set of root objects (GC Roots) and traverse the object graph along reference relationships.
Common root objects include:
- Valid references in the current thread stack.
- References from static variables.
- References associated with active threads.
- Certain references held internally by the virtual machine.
Objects that cannot be reached from root objects can generally be determined to be unreachable.
flowchart TB
A["GC Roots"] --> B["Object A"]
A --> C["Object B"]
B --> D["Object C"]
C --> E["Object D"]
D --> F["Object E"]
G["Object F"] --> H["Object G"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style D fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style E fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
style G fill:#FFF5DE,stroke:#E8D29A,color:#334155
style H fill:#FFF5DE,stroke:#E8D29A,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px