malloc/free 和 new/delete 的区别

时间:2023-03-22 12:20:56

1、malloc在C和C++中的区别

1.1、C中可以将任意的指针赋值给void*类型的变量。也可以将void*类型的值赋值给通常的指针变量。

-------------------------------------------------------------------------------------------------------
ANSI C 以前的C因为没有void*这样的类型,所以malloc的返回值类型被定义为char*。
char*是不能被赋给指向其他类型的指针变量的。所以必须进行下面这样的类型转换。
char *pch = (char*)malloc(size);
ANSI C中,malloc的返回值类型为void*, void*类型的指针可以不经过强制转化赋值给指向其他类型的指针变量。
因此,像上面的强制转换现在已经不需要了。
char *pch = malloc(size);
建议:在C中,不要对malloc的返回值进行强制类型转换,不写多余的代码。
-------------------------------------------------------------------------------------------------------
1.2、C++中可以将任意的指针赋值给void*类型的变量。但不可以将void*类型的值赋值给通常的指针变量。
所以C++中,必须对malloc的返回值进行强转。

1.3、结论
C中,不必对malloc的返回值进行强转
C++中,必须对malloc的返回值进行强转

2、new/delete 和malloc/free的区别

① malloc与free是C++/C语言的标准库函数,new/delete是C++的运算符;

② 对于非内部数据类型的对象而言,光用maloc/free无法满足动态对象的要求。对象在创建的同时要自动执行构造函数,对象在消亡之前要自动执行析构函数。由于malloc/free是库函数而不是运算符,不在编译器控制权限之内,不能够把执行构造函数和析构函数的任务强加于malloc/free,new可以认为是malloc加构造函数的执行。

③ new能够自动计算需要分配的内存大小,不需要明确指定,减少了出错的可能。
④ new不需要进行强制类型转换,能够自动返回正确的指针类型
⑤ new可以对分配的内存进行初始化(指的是 char* p = new char('A');这种情况)
⑥ new和delete可以被重载,程序员可以借此扩展new和delete的功能,建立自定义的存储分配系统,详见http://www.cnblogs.com/LubinLew/p/overload_new_and_delete.html

3、new 和 delete 的用法

① p = new type           //分配内存
② p = new type(value) //分配内存,并将内存初始化为value(value可以是类等)
③ p = new type[n]      //分配有n个元素的数组内存(size = n * sizeof(type))

① delete p                  //释放p指向的类型内存(对应new的①和②)
② delete []p               //释放p指向的数组内存(对应new的③)

4、malloc(0) ?

malloc() allocates size bytes and returns a pointer to the allocated memory.
The memory is not cleared. If size is 0, then malloc() returns either NULL,
or a unique pointer value that can later be successfully passed to free().
如果请求的长度为0,则标准C语言函数返回一个null指针或不能用于访问对象的非null指针。

5、calloc、realloc

头文件 :#include <stdlib.h>

函数原型:void *calloc(size_t nmemb, size_t size);

功 能: 在内存的动态存储区中分配n个长度为size的连续空间,函数返回一个指向分配起始地址的指针;如果分配不成功,返回NULL。

calloc在动态分配完内存后,自动初始化该内存空间为零,而malloc不初始化,里边数据是随机的垃圾数据。

函数原型:void *realloc(void *ptr, size_t size);

功 能:

【Linux man page】The realloc() function changes the size of the memory block pointed to by ptr to size bytes.

The contents will be unchanged in the range from the start of the region up to the minimum of the old and new sizes.

If the new size is larger than the old size, the added memory will not be initialized. If ptr is NULL, then the call is equivalent to malloc(size),

for all values of size; if size is equal to zero, and ptr is not NULL, then the call is equivalent to free(ptr). Unless ptr is NULL,

it must have been returned by an earlier call to malloc(), calloc() or realloc(). If the area pointed to was moved, a free(ptr) is done.

realloc() 函数的功能是改变 ptr 所指向内存块大小为size 个字节。

ptr指向的内存区域的开始到old size 和 new size的最小值这个范围内的值不会被改变。

如果新的size大于旧的size,增加的内存区域不会被初始化。如果 ptr 等于NULL, 那么等价于 malloc(size),

如果size 等于零并且ptr不等于NULL,那么等价于 free(ptr)。

除非ptr是NULL,否则ptr肯定是之前调用malloc(), calloc() 或者 realloc()的返回值。

【C90(C++98)】Otherwise, if size is zero, the memory previously allocated at ptr is deallocated as if a call to free was made, and anull pointer is returned.

如果size 等于零,等价于 free(ptr),并且返回一个空指针。

【C99/C11(C++11)】If size is zero, the return value depends on the particular library implementation: it may either be a null pointer or some other location that shall not be dereferenced.

如果size 等于零,返回值是否库的实现来决定的。可能返回NULL,也可能返回一个不应该被间接引用的位置。

6、new 和 delete 的深入了解及其工作机制

http://www.cnblogs.com/hazir/p/new_and_delete.html

---------------------------------------------------------------------------------------------------
参考资料:
<<征服C指针>>
<<C++面向对象程序设计(第二版)>>[P25]
http://www.cnblogs.com/wuyuegb2312/p/3219659.html

http://www.cnblogs.com/hazir/p/new_and_delete.html