一.C++内存分布
int globalVar = 1;
static int staticGlobalVar = 1;
void Test()
{
static int staticVar = 1;
int localVar = 1;
int num1[10] = { 1, 2, 3, 4 };
char char2[] = "abcd";
const char* pChar3 = "abcd";
int* ptr1 = (int*)malloc(sizeof(int) * 4);
int* ptr2 = (int*)calloc(4, sizeof(int));
int* ptr3 = (int*)realloc(ptr2, sizeof(int) * 4);
free(ptr1);
free(ptr3);
}
二.new 和 delete
new和delete都是C++中的操作符,使用方法也很简单,方便,下面让我们来看看它们底层是怎么实现的吧。
三.new和delete的底层实现
operator new与operator delete函数
总结
四.实现原理
下面以这段代码进行举例
int main()
{
//这是我们曾经写过的一个栈
//需要申请一个堆上的栈对象
Stack* ps = new Stack;
delete ps;
return 0;
}
图解: