2013-04-07 74 views
0

我正在嘗試開發一個使用兩個結構的動態分配的C循環緩衝區。一個擁有詳細信息,另一個主要用作從主循環到緩衝區結構的指針(因爲在運行時會分配多個數組)。C - 動態地分配結構中的結構的循環緩衝區

由於它是一個圓形緩衝器,我有一個指針「下一個」,它指向該陣列中的下一個項目(所以最後一個數組索引指向所述第一等)

這是兩個結構對象我有:

typedef struct { 
    int a; 
    int b; 
    struct1 *next; // pointer to next struct1 object in array 
} struct1; 

typedef struct { 
    struct1 *curr;  
    struct1 *start = NULL; 
    struct1 *end = NULL; 
} struct2; 

然後,我有我的初始化函數,從main調用來啓動一個新的循環緩衝區。

這是我不完全確定要做什麼的部分。

#define minSize 10 
struct2 * initialize() 
{ 
    struct2 **newBuf = malloc(sizeof(*newBuf)); 
    newBuf->malloc(sizeof(*newBuf->quotes) * newBuf->minSize); 

    // set the start pointer 
    newBuf.curr[0] = newBuf->start; 
    newBuf.curr[0]->next = NULL; 

    for (int i = 1; i < minSize; i++) 
    { 
     struct1 *new = NULL;  
     newBuf.curr[i] = new; // make index i = NULL 
     // have the previous index point to the "next" current 
     if (i > 0) 
      newBuf.curr[i-1]->next = newBuf.curr[i]; 
    } 

    // connect last index with first 
    newBuf.curr[minSize - 1]->next = newBuf.curr[0]; 

    // set the end pointer 
    newBuf->end = newBuf->start; 

    return newBuf; 
} 

從搜索,我發現this answer on how to initialize an array of structs within a struct通過使用malloc爲最初分配的空間,但也很困惑我的代碼將如何排隊,因爲我有一個指針來定義開始並在限定的圓形緩衝的年底 struct2,以及下一個指針作爲struct1的一部分。

此外,我選擇了定義*** newBuf *而不是** newBuf *,因爲我正在考慮它作爲指針的指針(考慮單鏈表)。雖然,如果我錯了,請糾正我。

我已經完成了Java中動態分配的循環緩衝區,但不是C和C++,所以我很難弄清楚如何初始化所有內容。我基本上被困在這個混亂,不知道下一步去哪裏。

任何可以給予的幫助將不勝感激!

+0

'typedef結構{ struct1 CURR []; '不計算。請發佈真實的代碼。也許你會得到真正的答案。 – wildplasser 2013-04-07 23:24:22

+0

是的,wildplasser:http://en.wikipedia.org/wiki/Typedef – 2013-04-07 23:35:27

+1

沒有@RobG。 OP *可能意味着一個VLA,但它僅作爲結構中的最後一個元素有效。無論如何:他糾正了它。結論:它是*不是真實的代碼*。 – wildplasser 2013-04-07 23:43:49

回答

1

你遇到麻煩的原因是你試圖讓指針指向一個指針,而不是僅僅使用一個普通的指針。你想訪問包含在第一個指針指向的地址處的指針。按照原則,您試圖訪問原始指針地址的內存空間之外的成員(該地址只與地址一樣大)。然後你遇到了麻煩,因爲你還沒有初始化你的數組'curr'。我做的另一件事並不重要,但是可以幫助你理解指針,使你的數組​​成爲一個指針 - 這就是數組在C中的工作方式。數組只是數組第一個成員的地址,當你索引到數組,它只是將一個偏移量添加到該地址= index * sizeof(yourstruct)。

你想要的是

typedef struct { 
    struct1 *curr;  
    struct1 *start = NULL; 
    struct1 *end = NULL; 
} struct2; 

#define minSize 10 
struct2* initialize() 
{ 
struct2 *newBuf = (struct2 *) malloc(sizeof(struct2)); 
newBuf->curr = (struct1 *) malloc(sizeof(struct1) * minSize); 

// set the start pointer 
newBuf.curr[0] = newBuf->start; 
newBuf.curr[0]->next = NULL; 

for (int i = 1; i < minSize; i++) 
{ 
    struct1 *new = (struct1 *) malloc(sizeof(struct1)); 
    newBuf.curr[i] = new; 
    newBuf.curr[i-1]->next = newBuf.curr[i]; 
} 
    // connect last index with first 
    newBuf.curr[minSize - 1]->next = newBuf.curr[0]; 
    // set the end pointer 
    newBuf->end = newBuf->start; 
    return newBuf; 
} 
+0

我明白了,所以forloop中的每個調用都需要在內存中分配。這很有道理。這清除了我在具有內存分配的同一文件中遇到的許多其他問題。謝謝! – 2013-04-07 23:38:03

+1

沒有理由來轉換從malloc()獲得的返回值。 (並且通常愚蠢的理由是,允許C++編譯器接受代碼被'new'作爲變量名的存在而被刪除)。另外,成語'ptr = malloc(cnt * sizeof * ptr);'在OP比你的'ptr = malloc(cnt * sizeof(Type));'更健壯。 – wildplasser 2013-04-08 00:20:35