new/delete和malloc/free的区别是什么?
相关(重复?):在什么情况下我使用malloc vs new?
new/delete和malloc/free的区别是什么?
相关(重复?):在什么情况下我使用malloc vs new?
当前回答
新建/删除
Allocate / release memory Memory allocated from 'Free Store'. Returns a fully typed pointer. new (standard version) never returns a NULL (will throw on failure). Are called with Type-ID (compiler calculates the size). Has a version explicitly to handle arrays. Reallocating (to get more space) not handled intuitively (because of copy constructor). Whether they call malloc / free is implementation defined. Can add a new memory allocator to deal with low memory (std::set_new_handler). operator new / operator delete can be overridden legally. Constructor / destructor used to initialize / destroy the object.
Malloc / free
分配/释放内存 从“堆”分配内存。 返回一个void*。 失败时返回NULL。 必须以字节为单位指定所需的大小。 分配数组需要手动计算空间。 分配更大的内存块很简单(不需要担心复制构造函数)。 它们不会调用new / delete。 无法将用户代码拼接到分配序列中以帮助解决低内存问题。 malloc / free不能被合法覆盖。
特点对比表:
Feature | new / delete |
malloc / free |
---|---|---|
Memory allocated from | 'Free Store' | 'Heap' |
Returns | Fully typed pointer | void* |
On failure | Throws (never returns NULL ) |
Returns NULL |
Required size | Calculated by compiler | Must be specified in bytes |
Handling arrays | Has an explicit version | Requires manual calculations |
Reallocating | Not handled intuitively | Simple (no copy constructor) |
Call of reverse | Implementation defined | No |
Low memory cases | Can add a new memory allocator | Not handled by user code |
Overridable | Yes | No |
Use of constructor / destructor | Yes | No |
从技术上讲,由new分配的内存来自“Free Store”,而由malloc分配的内存来自“Heap”。这两个区域是否相同是一个实现细节,这是malloc和new不能混合使用的另一个原因。
其他回答
新建/删除
Allocate / release memory Memory allocated from 'Free Store'. Returns a fully typed pointer. new (standard version) never returns a NULL (will throw on failure). Are called with Type-ID (compiler calculates the size). Has a version explicitly to handle arrays. Reallocating (to get more space) not handled intuitively (because of copy constructor). Whether they call malloc / free is implementation defined. Can add a new memory allocator to deal with low memory (std::set_new_handler). operator new / operator delete can be overridden legally. Constructor / destructor used to initialize / destroy the object.
Malloc / free
分配/释放内存 从“堆”分配内存。 返回一个void*。 失败时返回NULL。 必须以字节为单位指定所需的大小。 分配数组需要手动计算空间。 分配更大的内存块很简单(不需要担心复制构造函数)。 它们不会调用new / delete。 无法将用户代码拼接到分配序列中以帮助解决低内存问题。 malloc / free不能被合法覆盖。
特点对比表:
Feature | new / delete |
malloc / free |
---|---|---|
Memory allocated from | 'Free Store' | 'Heap' |
Returns | Fully typed pointer | void* |
On failure | Throws (never returns NULL ) |
Returns NULL |
Required size | Calculated by compiler | Must be specified in bytes |
Handling arrays | Has an explicit version | Requires manual calculations |
Reallocating | Not handled intuitively | Simple (no copy constructor) |
Call of reverse | Implementation defined | No |
Low memory cases | Can add a new memory allocator | Not handled by user code |
Overridable | Yes | No |
Use of constructor / destructor | Yes | No |
从技术上讲,由new分配的内存来自“Free Store”,而由malloc分配的内存来自“Heap”。这两个区域是否相同是一个实现细节,这是malloc和new不能混合使用的另一个原因。
new和delete是c++原语,用于声明一个类的新实例或删除它(从而为该实例调用类的析构函数)。
malloc和free是C函数,它们分配和释放内存块(大小)。
两者都使用堆来进行分配。尽管如此,Malloc和free更“低级”,因为它们只是保留了一块可能与指针相关的内存空间。在该内存周围不会创建任何结构(除非您将C数组视为结构)。
new/delete是c++, malloc/free来自优秀的老C。
在c++中,new调用对象的构造函数,delete调用析构函数。
malloc和free来自于OO之前的黑暗时代,它们只分配和释放内存,而不执行对象的任何代码。
new和malloc之间的主要区别是new调用对象的构造函数,而对应的delete调用对象的析构函数。
还有其他不同之处:
New是类型安全的,malloc返回void*类型的对象 new在错误时抛出异常,malloc返回NULL并设置errno New是运算符,可以重载,malloc是函数,不能重载 New[]分配数组,比malloc更直观和类型安全 malloc派生的分配可以通过realloc调整大小,新派生的分配不能调整大小 malloc可以分配一个n字节的内存块,new必须被要求分配一个数组,比如说char类型
看区别,总结是malloc是C式的,new是c++式的。使用适合您代码库的版本。
虽然使用不同的内存分配算法实现new和malloc是合法的,但在大多数系统上,new是使用malloc在内部实现的,不会产生系统级的差异。
New是一个操作符,malloc()是一个函数。 New返回精确的数据类型,而malloc()返回void * (void类型指针)。 Malloc(),内存没有初始化,默认值是垃圾,而在new的情况下,内存用默认值初始化,就像int的情况下用'zero(0)'。 delete和free()都可以用于'NULL'指针。