new/delete是C++中管理对象生命周期的核心机制,malloc/free仅分配/释放原始内存。new在分配后自动调用构造函数,delete在释放前调用析构函数,确保对象正确初始化与资源清理;而malloc返回void*需手动转换且不调用构造函数,free不调用析构函数易导致资源泄漏。new通过抛出std::bad_alloc异常处理分配失败,支持nothrow版本返回nullptr;malloc则返回NULL需显式检查。此外,operator new/delete可重载以实现自定义内存池、对齐或调试,而malloc/free作为C库函数无法重载。因此,C++中应优先使用new/delete管理对象,malloc/free仅适用于与C兼容、原始内存操作或底层自定义分配器场景。
C++ 中
malloc
free
new
delete
malloc
free
new
delete
谈到 C++ 中的内存管理,
malloc
free
new
delete
最直接的差异,也是最容易被忽视的,就是类型安全。
malloc
void*
int* p = (int*)malloc(sizeof(int));
new
int* p = new int;
再深一层看,它们的本质区别在于对对象生命周期的管理。这是 C++ 区别于 C 的核心特性之一。当你
new
delete
立即学习“C语言免费学习笔记(深入)”;
而
malloc
free
malloc
malloc
free
此外,错误处理机制也不同。
malloc
NULL
new
std::bad_alloc
try-catch
if (ptr == NULL)
new
nothrow
int* p = new (std::nothrow) int;
NULL
最后,可重载性也是一个关键点。
new
delete
operator new
operator delete
malloc
free
new
delete
malloc
free
在 C++ 编程中,几乎所有需要动态分配内存来存储对象实例的场景,都应该毫无疑问地优先选择
new
delete
最主要的原因是,
new
delete
new
delete
想象一下,如果一个
std::string
malloc
malloc
free
那么,有没有
malloc
free
malloc
malloc
malloc
std::vector<char>
std::unique_ptr<char[]>
malloc
free
malloc
placement new
new
总而言之,对于 C++ 对象,
new
delete
malloc
free
new
delete
malloc
free
理解
new
delete
malloc
free
当我们使用
new
new
operator new
malloc
new
new
举个例子:
class MyClass { public: int* data; MyClass() { data = new int[10]; // 构造函数中分配资源 std::cout << "MyClass constructor called." << std::endl; } ~MyClass() { delete[] data; // 析构函数中释放资源 std::cout << "MyClass destructor called." << std::endl; } }; // 使用 new 创建对象 MyClass* obj = new MyClass(); // ... 使用 obj ...
当
new MyClass()
MyClass
data
int
与此对应,当我们使用
delete
delete
MyClass
data
delete
operator delete
delete obj; // 调用 MyClass 的析构函数,然后释放内存
当
delete obj
MyClass
data
obj
MyClass
现在,我们来看看
malloc
free
// 使用 malloc 分配内存(错误示范,除非是 placement new) MyClass* obj_malloc = (MyClass*)malloc(sizeof(MyClass)); // 此时 obj_malloc 指向的内存是原始的、未初始化的。 // MyClass 的构造函数根本没有被调用!data 指针是随机值。 // 尝试访问 obj_malloc->data 将导致未定义行为或崩溃。 // ... 假设你强行使用这块内存 ... free(obj_malloc); // 只是释放内存。MyClass 的析构函数根本没有被调用! // 如果 MyClass 内部有动态分配的资源(比如 data),它们将永远不会被释放,造成内存泄露。
malloc
free
所以,核心的根本不同在于:
new
delete
malloc
free
new
delete
new
new
malloc
free
new
当
new
std::bad_alloc
try-catch
NULL
try { int* large_array = new int[1000000000ULL]; // 尝试分配一个巨大的数组 // ... 使用 large_array ... delete[] large_array; } catch (const std::bad_alloc& e) { std::cerr << "内存分配失败: " << e.what() << std::endl; // 可以在这里执行清理工作,或者尝试其他策略 }
这种异常处理方式比
malloc
NULL
new
if (ptr == nullptr)
当然,C++ 也提供了
new (std::nothrow)
nullptr
malloc
new
delete
这是
new
delete
malloc
free
operator new
operator delete
全局重载: 你可以重载全局的
operator new
operator delete
new
operator new
operator delete
// 示例:一个简化的全局 operator new/delete 重载 void* operator new(std::size_t size) { std::cout << "全局 operator new 被调用,分配 " << size << " 字节" << std::endl; if (void* ptr = std::malloc(size)) { // 底层仍然可能使用 malloc return ptr; } throw std::bad_alloc(); } void operator delete(void* ptr) noexcept { std::cout << "全局 operator delete 被调用" << std::endl; std::free(ptr); }
通过这种方式,每次你使用
new
operator new
类级别重载: 你也可以为特定的类重载
operator new
operator delete
operator new
class MyAlignedClass { public: int data[4]; // 假设需要16字节对齐 // 重载类级别的 operator new/delete void* operator new(std::size_t size) { // 假设这里实现了一个自定义的16字节对齐内存分配器 void* ptr = _aligned_malloc(size, 16); // 示例,具体实现可能不同 if (!ptr) throw std::bad_alloc(); std::cout << "MyAlignedClass 的 operator new 被调用,分配 " << size << " 字节" << std::endl; return ptr; } void operator delete(void* ptr) noexcept { std::cout << "MyAlignedClass 的 operator delete 被调用" << std::endl; _aligned_free(ptr); // 示例 } };
这种可重载性赋予了 C++ 开发者极大的灵活性和控制力,能够根据应用程序的特定需求,精细化地管理内存,实现高性能、高可靠性的系统。而
malloc
free
new
delete
以上就是C++中C语言的malloc/free和new/delete有什么本质区别的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号