malloc/free new/delet 的区别

本文详细对比了C++中new/delete与malloc/free的区别,包括对象构造与析构的处理方式、类型转换、异常处理等方面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

When new has an object, space for the object is not only allocated but the object's constructor is called. And similarly when delete as an object, the object's destructor is called before the memory is released. If malloc and free are used, the destructor and constructor do not get called respectively and obviously, this simply won't do in C++ except in certain very rare situations where classes are present without any specific destructor/constructors.

                      Operator new automatically calculates the size of the object that it constructs. Conversely, with malloc(), the programmer has to specify explicitly the number of bytes that have to be allocated. In addition, malloc() returns void *, which has to be explicitly cast to the desired type. This is both tedious and dangerous. Operator new returns a pointer to the desired type, so no explicit typecast is required.

                       new and delete automatically construct and destroy objects. malloc() and free(), on the other hand, merely allocate and deallocate raw memory from the heap. delete can be overloaded, free cannot. delete invokes the destructor of the object to be deallocated, free does not do this. 

The difference could be qouted as -

1. Operator new constructs an object (calls constructor of object), malloc does not.

2. Operator new is an operator, malloc is a function.

3. Operator new can be overloaded, malloc cannot be overloaded.

4. Operator new throws an exception if there is not enough memory, malloc returns a NULL.

5. Operator new[] requires to specify the number of objects to allocate, malloc requires to specify the total number of bytes to allocate.               

6. malloc() returns void *, which has to be explicitly cast to the desired type but new returns the proper type.

7. Operator new/new[] must be matched with operator delete/delete[] to deallocate memory, malloc() must be matched with free() to deallocate memory.

8. The new/delete couple does not have a realloc alternative that is available when malloc/free pair is used. realloc is used to resize the length of an array or a memory block dynamically.

 
### 回答1: malloc/freenew/delete 都是C/C++中的内存管理函数。malloc/free 是C语言的内存管理函数,它们用于申请和释放内存空间,它们没有构造函数和析构函数,只能用于普通的内存管理。而new/delete是C++中的内存管理函数,它们用于申请和释放内存空间,它们有构造函数和析构函数,可以用于类的内存管理。 ### 回答2: malloc/freenew/delete是两种动态内存分配和释放的方式。 首先,malloc/free是C语言的标准库函数,而new/delete是C++中的操作符。 其次,mallocfree是通过调用标准库函数来分配和释放内存,而newdelete是通过调用运算符来完成。 此外,mallocfree只能用于分配和释放内存,它们并不能调用对象的构造函数和析构函数。而newdelete不仅可以分配和释放内存,还可以调用对象的构造函数和析构函数。 另外,malloc分配的内存空间的大小是以字节为单位的,而new分配的内存空间的大小是以对象为单位的。因此,使用malloc分配内存时,需要手动计算所需的空间大小并进行类型转换;而使用new分配内存时,会自动计算所需的空间大小,并进行类型检查。 最后,malloc/free返回的是void指针,需要进行类型转换后才能使用;而new返回的是对象的指针,不需要进行类型转换。 综上所述,malloc/freenew/delete区别在于语法和功能上的不同。对于C语言来说,只能使用mallocfree来进行动态内存分配和释放;而对于C++来说,则推荐使用newdelete来进行动态内存分配和释放,并能够调用对象的构造函数和析构函数。 ### 回答3: malloc/freenew/delete是两种在C和C++中用于内存分配和释放的方法。 首先,mallocfree是C语言中的函数,而newdelete是C++中的操作符。 mallocfree是库函数,用于动态分配和释放内存。它们需要手动指定要分配的内存大小,并且返回的是一个void指针。由于返回的是void指针,因此必须进行类型转换,以便使用分配的内存。此外,malloc分配的内存大小可以是0,而free不能接受空指针作为参数。 而newdelete是C++中的操作符。new操作符用于在堆上分配内存,并且会自动调用相应类型的构造函数进行对象的初始化。在使用new操作符时,不需要手动指定内存大小,而是需要指定要分配的类型。delete操作符用于释放new操作符分配的内存,并且会自动调用相应类型的析构函数进行对象的清理。同时,delete操作符还可以正确处理数组类型的对象的释放。 综上所述,malloc/freenew/delete区别包括以下几个方面: - 使用场景:malloc/free适用于C语言,而new/delete适用于C++语言。 - 操作符与函数:malloc/free是函数,需要手动指定内存大小,返回的是void指针;而new/delete是操作符,不需要指定内存大小,可以自动调用构造函数和析构函数。 - 初始化和清理:new操作符可以自动调用构造函数进行对象初始化,而delete操作符可以自动调用析构函数进行对象清理。 - 处理数组:delete操作符可以正确处理数组类型的对象的释放。 因此,在使用C++编程时,推荐使用new/delete来替代malloc/free,可以更方便地进行内存管理和对象的初始化和清理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值