C++new??delete???????
???????????? ???????[ 2015/5/27 14:03:14 ] ???????????????
????C++?е?new ??delete ?????????new ??????????????????????????delete????????????????????????????C?????? malloc???? ?? free???? ??????? ??????malloc free ?? new ??delete ?????????????ò??????????????????????????С?
????C++??new ??delete??????????£?
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include <iostream>
using namespace std;
//new ??delete???????
//malloc() ??free() ?????
//new ????????????????????????????????????
int main01() {
//new????????
int* p = (int*)malloc(sizeof(int));
free(p);
int* p2 = new int;
*p2 = 101;
printf("*p2:%d
"?? *p2);
delete p2;
//??????????????????????????????????100
int* p3 = new int(100);
system("pause");
return 0;
}
class Test {
public:
Test(int mya?? int myb) {
cout << "?????????????????" <<endl;
a = mya;
b = myb;
}
~Test() {
cout << "???????????????????" << endl;
}
int getA(){
return a;
}
protected:
private:
int a;
int b;
};
int main() {
//new????
int* p1 = (int*)malloc(10*sizeof(int)); //int a[10]
p1[0] = 1 ;
free(p1);
int* p2 = new int[10];
p2[0] = 1;
p2[1] = 2;
delete [] p2;
//new???? new????????????????????????????
//delete????????????????????????????????????
//?????????????????????
Test* p3 = new Test(1??2);
cout << p3->getA() << endl;
delete p3;
system("pause");
return 0;
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11