1、new/delete
1.跟踪反汇编,查看函数的封装:
malloc _nh_malloc_dbg _heap_alloc_dbg _heap_alloc_base HeapAlloc
new _nh_malloc _nh_malloc_dbg _heap_alloc_dbg _heap_alloc_base HeapAlloc
free _free_dbg _free_base HeapFree
delete _free_dbg
2.new 关键字
int* pi = new int;
int* pk = new int(5);
Person* ps = new Person;
Person* pd = new Person(1, 2);
第一步:_nh_malloc->_nh_malloc_dbg->_heap_alloc_dbg->_heap_alloc_base->HeapAlloc
第二步:调用构造函数
3.delete关键字
delete pi;
delete pk;
第一步:调用析构函数
第二步:_free_dbg->_free_base->HeapFree
4.new[] 与 delete[]的用法.
int* pi = new int[10];
delete[] pi;
Person* p = new Person[2];
delete[] p;
2、vector
#include<stdio.h>
#include<Windows.h>
#define SUCCESS 1 // 成功
#define ERROR -1 // 失败
#define MALLOC_ERROR -2 // 申请内存失败
#define INDEX_ERROR -3 // 错误的索引号
template <class T_ELE>
class Vector
{
public:
Vector();
Vector(DWORD dwSize);
~Vector();
public:
DWORD at(DWORD dwIndex, OUT T_ELE* pEle); //根据给定的索引得到元素
DWORD push_back(T_ELE Element); //将元素存储到容器最后一个位置
VOID pop_back();