error: Cannot access memory at address 0x5fc498f0

问题记录

在写linux下的代码时,遇到了如标题的报错
例子:
a.c:

static char* buf = "";

const char* get_buf() {
  return buf;
}

b.c:

void test() {
  printf("%s \n", get_buf());
}

编译后,执行test时,在遇到get_buf()时,在GDB中报了如标题的错误;直接运行则报段错误

使用GDB调试时,会发现get_buf返回的地址于buf的地址并不一致(非常奇怪)

解决:
在b.c中,在调用get_buf之前,先对get_buf进行声明,即可。

posted @ 2025-01-23 17:10  太极者  阅读(11)  评论(0编辑  收藏  举报