每日一题(21)——malloc与free(二)

xiaoxiao2021-02-28  116

char *GetMemory(void) { char p[] = "hello world"; return p; } void Test(void) { char *str = NULL; str = GetMemory(); printf(str); } 1、指出编程错误 2、指出错误后果 3、指出纠正方法

分析:

运行结果可能会乱码,因为GetMemory返回的是指向“栈内存”的指针,该指针的地址不是NULL,但其原先的内容已经被清除,新内容未知,所以可能乱码。

和上一题的处理修改方法可以一样,二级指针的形式。

转载请注明原文地址: https://www.6miu.com/read-65172.html

最新回复(0)