// 动态申请内存, 指向一个未初始化的整型 int *pi = new int;
// pi指向一个整型值,初始化为0 int *pi = new int();
// value of i is 1024 int i(1024);
// pi指向1024 int *pi = new int(1024);
// value of s is "99999" string s(5, ‘9‘);
// *ps is "999999" string *ps = new string(6, ‘9‘);
/* 只有使用new申请的内存,才能用delete来释放 */
int i = 100; int *pi = &i; double *pd = new double(100);
// error: pi 指向一个局部变量,没有使用new来申请内存 delete pi;
// ok delete pd;
C++在堆上申请和释放内存 - new & delete
时间: 2024-10-15 03:43:39