动态分配内存

时间:2023-01-29 22:06:18
动态分配内存:测试代码:#include #include using namespace std;class myclass{int num;char name[80];public:myclass(int x,char *s) {num=x;strcpy(name,s);}~myclass(){cout<<"Del class /n";}void show(){cout<<"num is "< show();*p=1;cl->set(*p);cl->show();delete p;delete cl;return 0;}测试结果:num is 0; name is yanghangjunnum is 1; name is yanghangjunDel classPress any key to continue测试代码:#include #include using namespace std;int main(){int *p;int i;p=new(nothrow) int[10]; //nothrow替代形式if(!p){cout<<"alloc memory failure/n";return 1;}for(i=0;i<10;i++)*(p+i)=i;for(i=0;i<10;i++)cout<