[英]Creating an array of pointers to structs is malloc needed?
我已經創建了這樣的結構
struct PCB
{
int used;
char PID[1];
struct RCB *Other_Resources;
char type[10];
struct PCB **list;
struct PCB *parent;
struct PCB *children;
struct PCB *next;
int priority;
};
然后我聲明了一個像這樣的struct PCB *數組
struct PCB *PCBLIST[1024];
我的問題是我需要malloc結構指針數組才能使用這些指針嗎? 我在這里讀到另一個問題,我應該這樣做:
PCBLIST = malloc(1024 * sizeof(struct PCB *));
這個被略微修改,我認為在最初的問題中他們使用MAX而不是給出實際值,就像我做了1024。
我從gcc c99得到的錯誤是:
error: assignment to expression with array type
但如果我嘗試訪問像PCBLIST [i] - >使用的單個結構指針,我會得到一個seg錯誤。 有人能否解釋我做錯了什么?
您不需要動態分配數組 ,因為數組的聲明已經分配了數組。 您需要為每個要指向的數組條目分配結構內存。 就像是:
int ix;
for (ix = 0; ix < sizeof(PCBLIST) / sizeof(PCBLIST[0]); ix++) {
PCBLIST[ix] = malloc(sizeof(struct PCB));
}
請注意,為數組的所有條目分配內存。 如果總是使用所有條目,那么更簡單的方法是通過將數組聲明為結構數組而不是指向這些結構的指針數組來使用靜態分配:
struct PCB PCBLIST[1024];
這簡化了事情,不需要動態分配。 但是以可能使用更多內存為代價,因為它不允許填充稀疏的數組(並非所有條目分配)。 哪一個最好使用取決於您打算如何使用該數組。
PCBLIST應該是一個指針,而不是一個數組,因為你想用malloc分配空間,並將地址存儲到一個指針。
並且您需要分配結構的大小而不是指針的大小。
struct PCB *PCBLIST;
PCBLIST = malloc(1024 * sizeof(struct PCB));
編輯:使用malloc是首選問題而非必須
以下是使用非動態分配的靜態節點執行此操作的一些示例。
在第一個我沒有改變你在問題中定義的位(所以你有一個指向PCB對象的指針數組 - 我不認為這是你所追求的,因為如果你使用的標志是沒用的僅創建您使用的節點:
#include <string.h> // strcpy
// the PCB structure definition
struct PCB
{
int used;
char PID[1];
struct RCB *Other_Resources;
char type[10];
struct PCB **list;
struct PCB *parent;
struct PCB *children;
struct PCB *next;
int priority;
};
// an uninitialized array of pointers to PCB structures
struct PCB *PCBLIST[1024];
// make some PCB objects
struct PCB pcbParent;
struct PCB pcbChild1;
struct PCB pcbChild2;
int main(void)
{
// initialize PCBLIST
for (int i = 0;i < 1024;i++)
PCBLIST[i] = NULL;
// initialize parent
pcbParent.used = 1;
pcbParent.PID[0] = 0;
pcbParent.Other_Resources = NULL;
strcpy(pcbParent.type,"Parent");
pcbParent.list = PCBLIST;
pcbParent.parent = NULL; // no parent
pcbParent.children = &pcbChild1;
pcbParent.next = NULL; // no next sibling
pcbParent.priority = 0;
PCBLIST[0] = &pcbParent;
// initialize child1
pcbChild1.used = 1;
pcbChild1.PID[0] = 1;
pcbChild1.Other_Resources = NULL;
strcpy(pcbChild1.type,"Child");
pcbChild1.list = PCBLIST;
pcbChild1.parent = &pcbParent;
pcbChild1.children = NULL; // no children
pcbChild1.next = &pcbChild2; // next sibling
pcbChild1.priority = 0;
PCBLIST[1] = &pcbChild1;
// initialize child2
pcbChild2.used = 1;
pcbChild2.PID[0] = 2;
pcbChild2.Other_Resources = NULL;
strcpy(pcbChild2.type,"Child");
pcbChild2.list = PCBLIST;
pcbChild2.parent = &pcbParent;
pcbChild2.children = NULL; // no children
pcbChild2.next = NULL; // no next sibling
pcbChild2.priority = 0;
PCBLIST[2] = &pcbChild2;
// do some stuff with the nodes
return 0;
}
這樣做是沒有多大意義的,因為你也可以直接制作PCB對象數組 - 在這種情況下你根本就不需要PCBLIST,因為列表是 PCB對象本身的數組。 例如,這個更有意義:
#include <string.h> // strcpy
// the PCB structure definition
struct PCB
{
int used;
char PID[1];
struct RCB *Other_Resources;
char type[10];
struct PCB *list;
struct PCB *parent;
struct PCB *children;
struct PCB *next;
int priority;
};
// an uninitialized array of PCB structures
struct PCB PCBLIST[1024];
int main(void)
{
// initialize PCBLIST
for (int i = 0;i < 1024;i++)
{
PCBLIST[i].used = 0;
PCBLIST[i].Other_Resources = NULL;
PCBLIST[i].list = PCBLIST;
PCBLIST[i].parent = NULL; // no parent by default
PCBLIST[i].children = NULL; // no children by default
PCBLIST[i].next = NULL; // no next sibling by default
}
// initialize parent
PCBLIST[0].used = 1;
PCBLIST[0].PID[0] = 0;
strcpy(PCBLIST[0].type,"Parent");
PCBLIST[0].children = &PCBLIST[1];
PCBLIST[0].priority = 0;
// initialize child1
PCBLIST[1].used = 1;
PCBLIST[1].PID[0] = 1;
strcpy(PCBLIST[1].type,"Child");
PCBLIST[1].parent = &PCBLIST[0];
PCBLIST[1].next = &PCBLIST[2]; // next sibling
PCBLIST[1].priority = 0;
// initialize child2
PCBLIST[2].used = 1;
PCBLIST[2].PID[0] = 2;
strcpy(PCBLIST[2].type,"Child");
PCBLIST[2].parent = &PCBLIST[0];
PCBLIST[2].priority = 0;
// do some stuff with the nodes
return 0;
}
不,你不需要malloc()
。 您可以初始化指針以指向結構的靜態實例。 如果使用動態分配,則應使用calloc()
,而不是malloc()
加算術。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.