简体   繁体   English

创建一个指向结构的指针数组是malloc吗?

[英]Creating an array of pointers to structs is malloc needed?

I have created a struct like this 我已经创建了这样的结构

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

then I declared an array of struct PCB * like this 然后我声明了一个像这样的struct PCB *数组

struct PCB *PCBLIST[1024];

my question is do I need to malloc the array of struct pointers in order to use those pointers? 我的问题是我需要malloc结构指针数组才能使用这些指针吗? I read on another question on here that I should do something like this: 我在这里读到另一个问题,我应该这样做:

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

this is modified slightly, I think in the original question they used MAX instead of giving the actual value like I did 1024. 这个被略微修改,我认为在最初的问题中他们使用MAX而不是给出实际值,就像我做了1024。

The error I am getting from gcc c99 is: 我从gcc c99得到的错误是:

error: assignment to expression with array type

but if I try to access an individual struct pointer like with PCBLIST[i]->used I get a seg fault. 但如果我尝试访问像PCBLIST [i] - >使用的单个结构指针,我会得到一个seg错误。 Can someone shed some light on what I am doing wrong? 有人能否解释我做错了什么?

You don't need to dynamically allocate the array as that is already allocated by the declaration of the array. 您不需要动态分配数组 ,因为数组的声明已经分配了数组。 You need to allocate struct memory for each of the array entries to point to . 您需要为每个要指向的数组条目分配结构内存。 Something like: 就像是:

int ix;
for (ix = 0; ix < sizeof(PCBLIST) / sizeof(PCBLIST[0]); ix++) {
    PCBLIST[ix] = malloc(sizeof(struct PCB));
}

Note that that allocates memory for all the entries of the array. 请注意,为数组的所有条目分配内存。 If all the entries are always used then a simpler way would be to use a static allocation by declaring the array as an array of structs rather than an array of pointers to those structs: 如果总是使用所有条目,那么更简单的方法是通过将数组声明为结构数组而不是指向这些结构的指针数组来使用静态分配:

struct PCB PCBLIST[1024];

That simplies things by not requiring dynamic allocation. 这简化了事情,不需要动态分配。 But at the cost of potentially using more memory as it does not allow for a sparsely populated array (not all entries allocated). 但是以可能使用更多内存为代价,因为它不允许填充稀疏的数组(并非所有条目分配)。 Which one is best to use depends on how you intend to use the array. 哪一个最好使用取决于您打算如何使用该数组。

PCBLIST should be a pointer, not an array, because you want to allocate space with malloc, and store the address to a pointer. PCBLIST应该是一个指针,而不是一个数组,因为你想用malloc分配空间,并将地址存储到一个指针。

and you need to allocate the size of the struct and not the size of the pointer. 并且您需要分配结构的大小而不是指针的大小。

struct PCB *PCBLIST;

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

edit: using malloc is matter of choice and not a must 编辑:使用malloc是首选问题而非必须

Here are some examples of doing it with static nodes that are not dynamically allocated. 以下是使用非动态分配的静态节点执行此操作的一些示例。

In the first one I don't change the bits you defined in your question (so you do have an array of pointers to PCB objects - I don't think the is what you were after though because the used flag is not useful if you only create the nodes you use: 在第一个我没有改变你在问题中定义的位(所以你有一个指向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;
}

It doesn't make much sense to do it that way because you might as well make an array of PCB objects directly - in that case you don't need PCBLIST at all because the list is the array of PCB objects themselves. 这样做是没有多大意义的,因为你也可以直接制作PCB对象数组 - 在这种情况下你根本就不需要PCBLIST,因为列表 PCB对象本身的数组。 for example this one makes more sense: 例如,这个更有意义:

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

No, you don't need malloc() . 不,你不需要malloc() You could initialize the pointers to point to static instances of the structs. 您可以初始化指针以指向结构的静态实例。 If you do use dynamic allocation, you should use calloc() , not malloc() plus arithmetic. 如果使用动态分配,则应使用calloc() ,而不是malloc()加算术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM