首页 > 代码库 > 关于 C++ 的 new、delete 与 C 的 malloc、free 的区别
关于 C++ 的 new、delete 与 C 的 malloc、free 的区别
因为平台实时库部分的封装有点小恶心,如果取数据,用户必须传入一个有效的buf的初始地址和长度,接口内部的实现会在长度不够的情况下自动realloc这块buf,所以用户的缓存必须是malloc出来的。部门的个别小伙儿喜欢用了new来分配内存,当然了buf的长度足够大,是不会出问题的,如果一开始分配的比较小,问题就来了。纠结于因为这个问题,老是会被问及C/C++内存管理的差异,特写了个小程序以验证(写的不是很严谨,仅为主题,分别在redhat 5.6 x86_64 + gcc-4.9.2,windows 7 x64 + gcc-4.9.2 编译运行通过):
代码:
#include <iostream>#include <typeinfo>#include <stdlib.h>#include <stdio.h>void t_cpu(){#define print_type(t) std::cout << "Size of " << #t << ": " << sizeof(t) << std::endl print_type(char); print_type(unsigned char); print_type(short); print_type(unsigned short); print_type(int); print_type(unsigned int); print_type(size_t); print_type(long); print_type(unsigned long); print_type(long long); print_type(unsigned long long); print_type(float); print_type(double); print_type(long double); print_type(void*);}void t_new(size_t sl){ class __T { public: int _1; int _2; __T(){ _1 = 1; _2 = 2; } ~__T(){ printf("~"); } void test() { _1 += _2; } }; __T *pchar = new __T[sl]; size_t *psize = (size_t*)(((char*)pchar)-sizeof(size_t)); printf("\nTest new and delete \nsize: %ld pchar: %p psize: %p *psize: %ld \n", sl, pchar, psize, *psize); delete []pchar; printf("\n");}void t_malloc(size_t sl){ class __T { public: int _1; int _2; __T(){ _1 = 1; _2 = 2; } ~__T(){ printf("~"); } void test() { _1 += _2; } }; printf("\nTest malloc and free \nsize of class __T: %ld \n", sizeof(class __T)); __T *pchar = (__T *)malloc(sl * sizeof(class __T)); size_t *psize = (size_t*)(((char*)pchar)-sizeof(size_t)); printf("size: %ld pchar: %p psize: %p *psize: %ld \n", sl, pchar, psize, *psize); free(pchar);}void t_new_free(size_t sl){ class __T { public: int _1; int _2; __T(){ _1 = 1; _2 = 2; } ~__T(){ printf("~"); } void test() { _1 += _2; } }; __T *pchar = new __T[sl]; size_t *psize = (size_t*)(((char*)pchar)-sizeof(size_t)); printf("\nTest new and free \nsize: %ld pchar: %p psize: %p *psize: %ld \n", sl, pchar, psize, *psize); //delete []pchar; for (__T *t = pchar; t < pchar + sl; ++t) { t->~__T(); } free(psize); printf("\n");}void t_malloc_delete(size_t sl){ class __T { public: int _1; int _2; __T(){ _1 = 1; _2 = 2; } ~__T(){ printf("~"); } void init() { _1 = 7; _2 = 8; } void test() { _1 += _2; } }; printf("\nTest malloc and delete \nsize of class __T: %ld \n", sizeof(class __T)); size_t *psize = (size_t *)malloc(sl * sizeof(class __T) + sizeof(size_t)); __T *pchar = (__T*)(((char*)psize)+sizeof(size_t)); *psize = sl; //for (__T *t = pchar; t < pchar + sl; ++t) { // t->init(); //} printf("size: %ld pchar: %p psize: %p *psize: %ld \n", sl, pchar, psize, *psize); delete [] pchar; printf("\n");}int main(){ t_cpu(); t_new(48); t_malloc(32); t_new_free(16); t_malloc_delete(8); return 0;}
输出(redhat):
Size of char: 1Size of unsigned char: 1Size of short: 2Size of unsigned short: 2Size of int: 4Size of unsigned int: 4Size of size_t: 8Size of long: 8Size of unsigned long: 8Size of long long: 8Size of unsigned long long: 8Size of float: 4Size of double: 8Size of long double: 16Size of void*: 8Test new and delete size: 48 pchar: 0x2442018 psize: 0x2442010 *psize: 48 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Test malloc and free size of class __T: 8 size: 32 pchar: 0x2442010 psize: 0x2442008 *psize: 273 Test new and free size: 16 pchar: 0x2442018 psize: 0x2442010 *psize: 16 ~~~~~~~~~~~~~~~~Test malloc and delete size of class __T: 8 size: 8 pchar: 0x2442018 psize: 0x2442010 *psize: 8 ~~~~~~~~
输出(Windows):
Size of char: 1Size of unsigned char: 1Size of short: 2Size of unsigned short: 2Size of int: 4Size of unsigned int: 4Size of size_t: 4Size of long: 4Size of unsigned long: 4Size of long long: 8Size of unsigned long long: 8Size of float: 4Size of double: 8Size of long double: 12Size of void*: 4Test new and deletesize: 48 pchar: 00991b54 psize: 00991b50 *psize: 48~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Test malloc and freesize of class __T: 8size: 32 pchar: 00991b50 psize: 00991b4c *psize: 134253675Test new and freesize: 16 pchar: 00991b54 psize: 00991b50 *psize: 16~~~~~~~~~~~~~~~~Test malloc and deletesize of class __T: 8size: 8 pchar: 00991b54 psize: 00991b50 *psize: 8~~~~~~~~
关于 C++ 的 new、delete 与 C 的 malloc、free 的区别
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。