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

标签 c pointers recursion

我的 C 程序中遇到了一个非常奇怪的错误,因此我需要你们的帮助!所以我有一个称为路径的递归结构,有时我将“父”路径的地址存储在结构字段 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'd 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);

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

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

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;
}

关于c - 如何使用指针访问递归结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34162440/

相关文章:

c - 如何去掉这段代码中的 1.#J 输出?

c - Netbeans 正则表达式 - 查找和替换 (Ctrl + H)

c - 如何指向预分配内存上的 2D/3D 空间

c - 当您处理 C 和其他具有指针的语言时,初始化指针会自动将其设置为 null 吗?

c - 数组段错误

javascript - javascript 中递归的转换

使用C中的fork()并行计算文件夹中每个文本文件的行数

c - 将 3 个字节拆分为六个 4 位并重新组合为两个 12 位

c - 涉及静态局部变量的递归函数中的意外输出

linux - linux 文件搜索中的递归 - Bash 脚本