小编use*_*871的帖子

如何释放函数返回的指针?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char* f(void) {
  char *x;
  x = malloc(sizeof(char) * 4);
  strcpy(x, "abc");
  return(x);
}

int main(void) {
  char *a;
  a = f();
  printf("%s", a);
  free(a);
  return(0);
}
Run Code Online (Sandbox Code Playgroud)

是否x必须释放函数中的变量?如果是这样,当我需要退货时,这怎么可能呢?

c memory malloc free dynamic

11
推荐指数
1
解决办法
8789
查看次数

标签 统计

c ×1

dynamic ×1

free ×1

malloc ×1

memory ×1