-
-
-
-
void GetMemory1(char **p,int num){
*p=(char*)malloc(sizeof(char)*num);
}
void GetMemory2(char *&p,int num){
p=(char*)malloc(sizeof(char)*num);
}
char* GetMemory3(int num){
char *p=(char*)malloc(sizeof(char)*num);
return p;
}
int _tmain(int argc, _TCHAR* argv[])
{
char *str1=NULL;
char* str2=NULL;
char *str3=NULL;
char *str4=NULL;
GetMemory1(&str1,20);
GetMemory2(str2,40);
str3=GetMemory3(20);
strcpy(str1,"GetMemory1");
strcpy(str2,"GetMemory2");
strcpy(str3,"GetMemory3");
cout<<"str1="<<str1<<endl;
cout<<"str2="<<str2<<endl;
cout<<"str3="<<str3<<endl;
cout<<"str1==null?"<<(str4==NULL?"yes":"no")<<endl;
free(str1); //释放内存,并把指针赋为NULL
free(str2);
free(str3);
str1=NULL;
str2=NULL;
str3=NULL;
getchar();
return 0;
}