手动内存管理
你自己实现堆内存分配和释放。
稍有不慎,这可能会导致崩溃、bug、安全漏洞和内存泄漏。
C++ 示例
你必须对使用 malloc
分配的每个指针调用 free
:
void foo(size_t n) {
int* int_array = malloc(n * sizeof(int));
//
// ... lots of code
//
free(int_array);
}
Memory is leaked if the function returns early between malloc
and free
: the pointer is lost and we cannot deallocate the memory. Worse, freeing the pointer twice, or accessing a freed pointer can lead to exploitable security vulnerabilities.