gcc:释放结构后,结构中字符数组的值保持不变

gcc:释放结构后,结构中字符数组的值保持不变,c,gcc,struct,C,Gcc,Struct,我正在为一个项目编写代码,发现了一些bug,所以我编写了一个小程序来复制和测试它,下面是代码 #include <stdio.h> #include <stdlib.h> #include <string.h> struct str_a { int a; long b; char buf[10]; }; int main() { struct str_a *str1 = malloc(sizeof(struct str_a)); pri

我正在为一个项目编写代码,发现了一些bug,所以我编写了一个小程序来复制和测试它,下面是代码

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

struct str_a {
  int a;
  long b;
  char buf[10];
};

int main() {
  struct str_a *str1 = malloc(sizeof(struct str_a));
  printf("%p\n", str1);
  strncpy(str1->buf, "hello", sizeof(str1->buf));

  free(str1);

  struct str_a *str3 = malloc(sizeof(struct str_a));
  printf("%p, %s\n", str3, str3->buf);

  struct str_a *str5 = malloc(sizeof(struct str_a));
  printf("%p, %s\n", str5, str5->buf);

  return 0;
}

这就是问题所在,str3->buf是hello?我在我的项目中发现了同样的问题。我也用gdb来调试它,我看到freestr1之后,str1->buf仍然是hello。所以我在网上搜索发现它说freetestPerson就足够了,所以问题是什么,我也在vc++中测试了它,str3->buf输出了一些垃圾值,我认为这是正确的。那么,为什么gcc的行为是这样的

您希望free做什么?清除缓冲区?这不是它的工作。比较这里,我不认为我曾经结束了一个问题,提供了4个实际上更多的副本。。。这可能与通常的i++=i+++i序列点问题的频率相同。
0x139b010
0x139b010, hello
0x139b040,