c-在object_list_print()函数期间出现段错误?

时间:2014-02-21 19:06:24

标签: c linked-list segmentation-fault


FIXED:我在更改指针地址

之前添加了一个快速if语句

我的程序是行错误的

obj = (object_t *) model->objs->current->entity;

在我的object_list_print()函数中,看起来像这样。

// does not call object_print() directly, but instead
// will call the polymorphic function obj->printer() 
void object_list_print(model_t  *model, FILE  *out)
{
   object_t *obj;

   /* For each object in model->objs 
           list, assert that it is an object, 
                then invoke polymorphic printer    */
   list_reset(model->objs);
   obj = (object_t *)model->objs->current->entity;

   while(list_not_end(model->objs))
   {
      assert(obj->cookie == OBJ_COOKIE);
      obj->printer(obj, out);
      list_next_link(model->objs);
           // ADDED LINE //
      if(list_not_end(model->objs))         /* This line fixes the code */
         obj = (object_t *)model->objs->current->entity;  /* SEG FAULTS */
   }

   return;
}

作为参考,model-> objs是list_t结构

typedef struct list_type
{
   link_t  *first;           /* First link in the list */
   link_t  *last;            /* Last link in the list  */
   link_t  *current;
}  list_t;

,每个link_t如下所示:

typedef struct link_type
{
   struct link_type *next;    /* Next link in the list   */
   void             *entity;  /* Entity owned by link    */
}  link_t;

知道我在这里做错了吗?我的语法错了吗?

0 个答案:

没有答案
相关问题