Segfault - 我的错误在哪里?

时间:2014-03-14 11:02:23

标签: c segmentation-fault glib

我在writeLogs例程中遇到了段错误。

(gdb) print *analyzers->analyzer2.log
Cannot access memory at address 0x80000007f5b4
(gdb) print analyzers->analyzer2.log
$13 = (int *) 0x80000007f5b4

所以,指针似乎存在,价值,而不是。 我希望,我在每个例程中都有相同的指针和相同的值,我称之为。见 附加代码:

typedef struct analyzer_t
{     
  int *log;
}analyzer_t;


typedef struct analyzers_t
{
  analyzer_t analyzer2;
}analyzers_t;



int MarketTalkMessagesVerarbeiten(unsigned char *datap, size_t size, GTree* t, analyzers_t *analyzers)
{
    *analyzers->analyzer2.log++;
}
int writeLogs(char *name, analyzers_t *analyzers)
{
   printf("Anal: %d\n", *analyzers->analyzer2.log );  
}
gboolean main_loop(mainloop_param_t *data)
{
   analyzers_t *analyzers = data->analyzers;
   MarketTalkMessagesVerarbeiten(pUncompressStreamData, sizeBMB, t, analyzers);
   writeLogs(namelist[i]->d_name,  analyzers);
}
int main()
{
  int log_a2                = 0;
  analyzers_t analyzers;
  analyzers.analyzer2.log   = &log_a2;
  mainloop_param_t mlparams;
  mlparams.analyzers = &analyzers;
  GMainLoop* loop = g_main_loop_new (NULL, FALSE);

  g_timeout_add (5000, (GSourceFunc)main_loop, &mlparams);
  g_main_loop_run (loop);

}

1 个答案:

答案 0 :(得分:2)

++*更紧密。

所以

  *analyzers->analyzer2.log++;
  1. 增加...log(一个指针!)
  2. 取消引用1的结果(很可能指向无效的内存,因此解除引用它因此会调用未定义的行为,并导致观察到的崩溃)
  3. 要增加...log指向的位置,请执行:

    (*analyzers->analyzer2.log)++;
    

    那是:

    1. 取消引用analyzers->analyzer2.log
    2. 增加到第1步指向