----------------------------------------------------------------------------------------------------
void GetMemory(char *p, int num)//zbf:
{
p = (char *)malloc(sizeof(char) * num);
}
void Test(void)
{
char *str = NULL;
GetMemory(str, 100); // str
strcpy(str, "hello"); //
}
----------------------------------------------------------------------------------------------------
示例7-4-1
毛病出在函数GetMemory
如果非得要用指针参数去申请内存,那么应该改用“
---------------------------------------------------------------------------------------------------
void GetMemory2(char **p, int num)
{
*p = (char *)malloc(sizeof(char) * num);
}
void Test2(void)
{
char *str = NULL;
GetMemory2(&str, 100); //
strcpy(str, "hello");
cout<< str << endl;
free(str);
}
---------------------------------------------------------------------------------------------------
示例7-4-2
由于“
---------------------------------------------------------------------------------------------------
char *GetMemory3(int num)
{
char *p = (char *)malloc(sizeof(char) * num);
return p;
}
void Test3(void)
{
char *str = NULL;
str = GetMemory3(100);
strcpy(str, "hello");
cout<< str << endl;
free(str);
}
----------------------------------------------------------------------------------------------------
示例7-4-3
用函数返回值来传递动态内存这种方法虽然好用,但是常常有人把return
---------------------------------------------------------------------------------------------------
char *GetString(void)
{
char p[] = "hello world";//
return p; //
}
void Test4(void)
{
char *str = NULL;
str = GetString(); // str
cout<< str << endl;
}
---------------------------------------------------------------------------------------------------
示例7-4-4 return
用调试器逐步跟踪Test4
如果把示例7-4-4
--------------------------------------------------------------------------------------------------
char *GetString2(void)
{
char *p = "hello world";//
return p;
}
void Test5(void)
{
char *str = NULL;
str = GetString2();
cout<< str << endl;
}
----------------------------------------------------------------------------------------------
示例7-4-5 return
函数Test5
程序运行如下:
vc->File->new->c++source file
#i nclude "iostream.h"
#i nclude "stdio.h"
#i nclude "string.h"
#i nclude "malloc.h"
void GetMemory(char *p,int num);
void Test(void);
void GetMemory2(char **p,int num);
void Test2(void);
char *GetMemory3(int num);
void Test3(void);
char *GetString(void);
void Test4(void);
char *GetString2(void);
void Test5(void);
void main()
{
// Test();
Test2();
Test3();
Test4();
Test5();
}
void GetMemory(char *p,int num)//
{
p=(char*)malloc(sizeof(char)*num);
}
void Test(void)
{
char *str=NULL;
GetMemory(str,100);
strcpy(str,"hello");
cout<<str<<endl;
}
void GetMemory2(char **p,int num)
{
*p=(char*)malloc(sizeof(char)*num);
}
void Test2(void)
{
char *str=NULL;
GetMemory2(&str,100);//
strcpy(str,"hello");
cout<<str<<endl;
free(str);//
}
char *GetMemory3(int num)
{
char *p = (char*)malloc(sizeof(char)*num);
return p;
}
void Test3(void)
{
char *str=NULL;
str=GetMemory3(100);
strcpy(str,"hello");
cout<<str<<endl;
free(str);//
}
char *GetString(void)
{
char p[]="hello";
return p;//warning:return address of local variable or temporary(
}
void Test4(void)
{
char *str=NULL;
str=GetString();
cout<<"
cout<<str<<endl;
}
char *GetString2(void)
{
char *p="hello";
return p;
}
void Test5(void)
{
char *str=NULL;
str=GetString2();
cout<<str<<endl;
}
运行结果:
hello
hello
以下内容为垃圾:(随意的内容)
hello