首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否需要创建指向结构的指针数组?

是否需要创建指向结构的指针数组?
EN

Stack Overflow用户
提问于 2017-02-07 23:28:35
回答 4查看 783关注 0票数 3

我创建了这样一个结构

代码语言:javascript
复制
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;
};

然后我声明了一个结构件的数组*如下所示

代码语言:javascript
复制
struct PCB *PCBLIST[1024];

我的问题是,为了使用这些指针,我需要对结构指针数组进行malloc吗?我在这里读到了另一个问题,我应该这样做:

代码语言:javascript
复制
PCBLIST = malloc(1024 * sizeof(struct PCB *));

这是轻微的修改,我认为在最初的问题中,他们使用MAX,而不是像我1024那样给出实际值。

gcc c99的错误是:

代码语言:javascript
复制
error: assignment to expression with array type

但是,如果我试图访问单个的结构指针,比如使用PCBLISTi->used,我就会得到一个seg错误。有人能解释一下我做错了什么吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-02-07 23:35:17

不需要动态分配数组,因为数组的声明已经分配了数组。您需要为要指向的每个数组条目分配struct内存。类似于:

代码语言:javascript
复制
int ix;
for (ix = 0; ix < sizeof(PCBLIST) / sizeof(PCBLIST[0]); ix++) {
    PCBLIST[ix] = malloc(sizeof(struct PCB));
}

注意,它为数组的所有条目分配内存。如果总是使用所有条目,那么更简单的方法是将数组声明为结构数组而不是指向这些结构的指针数组,从而使用静态分配:

代码语言:javascript
复制
struct PCB PCBLIST[1024];

这通过不需要动态分配来简化事情。但代价是可能使用更多的内存,因为它不允许稀疏填充的数组(并非所有已分配的条目)。哪一个最适合使用取决于您打算如何使用数组。

票数 2
EN

Stack Overflow用户

发布于 2017-02-07 23:37:55

PCBLIST应该是指针,而不是数组,因为您希望用malloc分配空间,并将地址存储到指针。

您需要分配结构的大小,而不是指针的大小。

代码语言:javascript
复制
struct PCB *PCBLIST;

PCBLIST = malloc(1024 * sizeof(struct PCB));

编辑:使用malloc是一种选择,而不是必须的。

票数 0
EN

Stack Overflow用户

发布于 2017-02-08 01:08:26

下面是一些使用未动态分配的静态节点执行此操作的示例。

在第一个问题中,我不会更改您在问题中定义的位(因此,您确实有一组指向PCB对象的指针-但我不认为这是您所追求的,因为如果只创建您使用的节点,则使用的标志并不有用:

代码语言:javascript
复制
#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对象本身的数组。例如,这篇文章更有意义:

代码语言:javascript
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42101939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档