c-如何使用指针访问递归结构



我的C程序中出现了一个非常奇怪的错误,因此我需要你们的帮助!因此,我有一个名为path的递归结构,有时我会将"父"路径的地址存储在结构字段mother:中

 typedef struct path{
  struct path* mother;
  struct path** children;
  int length;
  uint8_t* inf;
 } path;

所以在我的例子中,我只生成一条路径,如下所示:

  int child_num=2;
  int bytes=10;
  path* my_path=malloc(sizeof(path));
  if (path==NULL) throw error...
  my_path->inf=malloc(sizeof(uint8_t)*bytes);
  memset(my_path->inf, 4, bytes);
  my_path->children=malloc(sizeof(path*)*child_num);
  for(int i=0; i<child_num; i++){
      my_path->children[i]->mother=my_path;
      my_path->children[i]->inf=malloc(sizeof(uint8_t)*bytes);
      memset(my_path->children[i]->inf, 5, bytes);
  }

所以现在,由于我存储了到父结构的链接,我想使用另一个帮助指针来访问它的信息:

  path* my_pointer=my_path->children[0]->mother;  //this is just for the example

所以我检查了地址,一切似乎都很好,但如果我知道在另一种方法中使用指针,指向字段"inf",如果我使用变量"path",它就会工作,所以:

     method(path->inf, bytes);

很好,但只要我这样做:

    method(my_pointer->inf, bytes);

该方法在标记线处崩溃:

 void method(uint8_t* element, int bytes) {
     if (element==NULL) ... //<=== here it crashes
     //do something

}

我真的不明白我做错了什么,我打印了地址,一切似乎都很好,即使我通过变量"my_pointer"访问某个字节,就像一样

      my_pointer->inf[1]

它会返回相应的值,但在单独的方法中它不起作用。

正如评论所示,我们无法用提供的信息准确回答您的问题,但我们可以为您指明正确的方向。

首先,我在您的示例中注意到,您使用path作为typedef的path结构的变量名。您需要对变量名称更加详细,或者实际复制粘贴一些代码,以确保我们能够查看实际问题,因为这可能只是命名的问题。

总而言之,我认为使用一点代码卫生会对你有很大的好处。在文件范围内组织一些用于数据结构开销的函数:

static int path_alloc(path* p);
static int path_alloc_kids(path* p, int num);
static int path_alloc(path* p) {
  if(p == NULL) { return -1; }
  p = (path*)malloc(sizeof(path));
  if(p == NULL) { return -2; }
  return 0;
}
static int path_alloc_kids(path* p, int num) {
  if(p == NULL || num <= 0) { return -1; }
  if(!path_alloc(p)) { /* Easier to read and understand, no error handling here to muddle things up */
    /* You don't actually need a path**, do you? Think of char *argv[] a.k.a. char **argv, is that what you're actually going for? */
    p->children = (path*)malloc(sizeof(path) * num);
    if(p->children == NULL) { return -2; }
    p->length = num;
  } else { return -1; } /* Simple */
  return 0;
}

这使得代码更容易理解,这是指针的主要问题。添加一些方法来释放分配的子级和根,您就可以以相对抽象的方式使用此路径结构。您可能需要考虑以链表的方式使用pathpath_node,这样您只分配您需要的内容。

struct spath_node; /* So it knows of itself */
typedef struct spath_node {
  struct spath_node *parent;
  struct spath_node *next;
  uint8_t *data;
  int data_size;
} path_node;

然后通过传递数据大小和父节点进行分配,NULL父节点可能意味着它是根节点。

static int path_alloc_node(path_node *parent, int data_size, uint8_t *data);

这使得插入/遍历相对较慢,但更容易理解哪里出了问题。

EDIT:需要明确的是,这就是我们将子项添加到链接列表的方式示例:

static int path_alloc_node(path_node *parent, int data_size, uint8_t *data) {
  path_node *tmp;
  if(parent == NULL || data_size <= 0) { return -1; }
  if(parent->next != NULL) { return -3; }
  tmp = (path_node*)malloc(sizeof(path_node));
  if(tmp == NULL) { return -2; }
  else parent->next = tmp;
  if(data == NULL) { /* Assume the caller is requesting a new data block of the given size */
    data = (uint8_t*)malloc((size_t)data_size);
    if(data == NULL) { return -2; }
  }
  parent->next->data = data;
  parent->next->data_size = data_size;
  parent->next->next = NULL;
  parent->next->parent = parent;
  return 0;
}

最新更新