每日一题(22)——malloc与free(三)

xiaoxiao2021-02-28  87

void GetMemory(char **p, int num) { *p = (char *)malloc(num); } void Test(void) { char *str = NULL; GetMemory(&str, 100); strcpy(str, "hello world"); printf(str); } 1、指出编程错误 2、指出错误后果

3、指出纠正方法

分析:

    内存只分配,而没有释放;

    能够输出hello world,但是会内存泄露;

    在Test最后加上free(str); str = NULL;即可。

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

最新回复(0)