// 程序员面试宝典 65页
// 在利用子函数实现内存堆分配的实现时出现的问题,同时也需要利用子函数实现内存堆的释放。
/*
#include<iostream>
using namespace std;
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(char *p,int num) // 使用还回值传递。
{
p=(char *)malloc(sizeof(char)*num);
return p;
}
int main()
{
//char *str= NULL; //char *str= (char*)0;
char *str= (char*)0;
//GetMemory1(str,100);// 导致程序崩溃。
//GetMemory2(&str,100);// 使用指针的指针传递指针
str=GetMemory3(str,100);// 使用还回值传递
strcpy(str,"hello world");
cout<<*str<<endl;
cout<<str<<endl;
cout<<&str<<endl;
// 任何时候,不要忘记了释放内存空间
free(str);
str=NULL;
return 0;
}
*/
// 在利用子函数实现内存堆分配的实现时出现的问题,同时也需要利用子函数实现内存堆的释放。
/*
#include<iostream>
using namespace std;
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(char *p,int num) // 使用还回值传递。
{
p=(char *)malloc(sizeof(char)*num);
return p;
}
int main()
{
//char *str= NULL; //char *str= (char*)0;
char *str= (char*)0;
//GetMemory1(str,100);// 导致程序崩溃。
//GetMemory2(&str,100);// 使用指针的指针传递指针
str=GetMemory3(str,100);// 使用还回值传递
strcpy(str,"hello world");
cout<<*str<<endl;
cout<<str<<endl;
cout<<&str<<endl;
// 任何时候,不要忘记了释放内存空间
free(str);
str=NULL;
return 0;
}
*/