void GetMem(char* pData){pData = new char[100];}char* pDDD = NULL;GetMem(pDDD);strcpy(pDDD,"hello");运行结果是什么

来源:学生作业帮助网 编辑:六六作业网 时间:2024/07/17 17:59:01
voidGetMem(char*pData){pData=newchar[100];}char*pDDD=NULL;GetMem(pDDD);strcpy(pDDD,"hello");运行结果是什么v

void GetMem(char* pData){pData = new char[100];}char* pDDD = NULL;GetMem(pDDD);strcpy(pDDD,"hello");运行结果是什么
void GetMem(char* pData)
{
pData = new char[100];
}
char* pDDD = NULL;
GetMem(pDDD);
strcpy(pDDD,"hello");
运行结果是什么

void GetMem(char* pData){pData = new char[100];}char* pDDD = NULL;GetMem(pDDD);strcpy(pDDD,"hello");运行结果是什么
传入的是值参,
在函数里的修改没有用.
所以pDDD在函数运行完之后还是 NULL
strcpy 这一句会出错.Runtime error.
而函数里new 的那块内存无法释放,会造成内存泄露.
void GetMem(char** ppData)
{
*ppData = new char[100];
}
char* pDDD = NULL;
GetMem(&pDDD);
strcpy(pDDD,"hello");
...
delete []pDDD;
这样还差不多.