Closed. This question is off-topic. It is not currently accepting answers. Learn more
想改进这个问题吗?Update the question所以堆栈溢出的值小于aa>。
两年前关闭。
#define MAX_SPACES 10
#define MAX_SIMPLE_EVENTS 5000
#define MAX_USER_EVENTS 1000

struct {
  EventSpace* p_spaces[MAX_SPACES];
  SimpleEvent* p_simple_events[MAX_SIMPLE_EVENTS];
  UserEvent* p_user_events[MAX_USER_EVENTS];
}* G_manager;

static void add_space(EventSpace* space){
  static uint16_t index = 0;
  (*G_manager).p_spaces[index] = space;
}

在gdb运行后得到:
程序接收信号SIGSEGV,分段故障。
添加空间中的0x0000000000400559(空间=0x7fffffffdf30)
如何保管?

最佳答案

至少在您显示的代码中,您没有为包含数组的结构实际分配内存;您只创建了一个指针。因此,当您解除对指针的引用,意味着写入结构的一个实例时,您击中了一个随机内存地址,得到了程序崩溃的相对温和的结果。
您可以将G_manager从指向结构的指针更改为结构本身的实例;也可以将足够大的缓冲区malloc为该结构并将该缓冲区分配给G_manager

关于c - Struct指针数组,在c中获取segfault ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42960600/

10-12 12:19
查看更多