C++里面new用来分配内存,delete用来释放内存,它们与C中的malloc和free有什么联系呢?
int* pi = (int*)malloc(sizeof(int) * 1);//分配四字节
free(pi);
//malloc->_nh_malloc_dbg->_heap_alloc_dbg->_heap_alloc_base->HeapAlloc
//free->_free_dbg->_free_base->HeapFree
int* pk = new int(5);//分配四字节,复制5
//第一步:_nh_malloc->_nh_malloc_dbg->_heap_alloc_dbg->_heap_alloc_base->HeapAlloc
//第二步:调用构造函数
delete pk;
//第一步:调用析构函数
//第二步:_free_dbg->_free_base->HeapFree
int* pj = new int[10];//分配40字节
delete[] pj;
参考:滴水视频