簡體   English   中英

如何使用指針訪問遞歸結構

[英]How to access recursive structure with pointers

我的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作為類型定義的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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM