请看一下下面这段代码有什么问题。
void GetMemory(char *p) {
p = (char *)malloc(100);
}
void Test(void) {
char *str = nullptr;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}
● 修改形参的值并不会改变实参的值,所以调用GetMemory
之后,str
依然为空指针;
● 在GetMemory
和Test
中,没有malloc
对应的free
,造成内存泄漏。