简体   繁体   English

C语言struct数组

[英]Array of struct C language

In my program, I have to create structs, in my case, I have struct Processor here is my example:在我的程序中,我必须创建结构,就我而言,我有结构处理器,这是我的示例:

struct Processor{
    int id;
    struct Task **task;
}

In the main I create set of processors (2 processors for example), and I have to add in tasks in each processor using this function struct Processor * create_processor(int id, struct Task **task );在主要我创建一组处理器(例如 2 个处理器),我必须使用这个函数在每个处理器中添加任务struct Processor * create_processor(int id, struct Task **task );

Here is the function:这是函数:

struct Processor * create_processor(int id, struct Task **task){
    struct Processor * pr = (struct Processor *) (malloc(sizeof(struct Processor)));
    pr->id = id;
    //how to initialize Task set??
    return pr;
}

Now how to initialize the set of tasks in the function?现在如何在函数中初始化任务集? and how to use elements of taskset?以及如何使用任务集的元素? I used processor[1]->task[1]->id = 5;我使用了processor[1]->task[1]->id = 5; but it returns ERROR Segmentation fault (core dumped)但它返回 ERROR Segmentation fault (core dumped)

Help please请帮忙

When you do当你做

struct Processor * pr = (struct Processor *) (malloc(sizeof(struct Processor)));

you've now initialized an empty Processor struct.您现在已经初始化了一个空的Processor结构。 Since the task member of the struct is a pointer (actually a double-pointer) all you have in your freshly initialized Processor is equivalent to: struct Processor { id: 0, task: 0 } , ie it just contains a null pointer for the task member.由于结构的task成员是一个指针(实际上是一个双指针),所以你在新初始化的Processor所拥有的一切等价于: struct Processor { id: 0, task: 0 } ,即它只包含一个空指针task成员。 That's probably why you get Segmentation fault , which is the type of error that occurs when your program accesses uninitialized memory.这可能就是您收到Segmentation fault ,这是您的程序访问未初始化内存时发生的错误类型。

You'll also have to malloc an array of Task* (task pointers) for however many tasks each processor has to have.您还必须为每个处理器必须拥有的许多任务分配一个Task* (任务指针)数组。 Eg例如

pr.task = (struct Task **) malloc(sizeof(struct Task *) * N_TASKS);  // pr.task should probably be called pr.tasks??

then you also need to initialize memory for each actual task:那么您还需要为每个实际任务初始化内存:

for (int idx = 0; idx < N_TASKS; idx++) {
    pr.task[idx] = (struct Task *) malloc(sizeof(struct Task));
}

and so on for each Processor you want to initialize.等等你想要初始化的每个Processor

Maybe this can help.也许这会有所帮助。 I suppose you allocate memory for tasks before calling your function.我想您在调用函数之前为任务分配内存。 Here you have 10 tasks and I create one processor with 3 tasks:这里有 10 个任务,我创建了一个包含 3 个任务的处理器:

struct Processor{
    int id;
    struct Task **task;
};

struct Task{ int id; int WCET;};

struct Processor * create_processor(int id, struct Task **task){
    struct Processor * pr = (struct Processor *) (malloc(sizeof(struct Processor)));
    pr->id = id;
    pr->task = task;
    return pr;
}


int main() {
    struct Task taskArray[10]; /* memory for 10 tasks */
    struct Task * taskPtrArray[3]; /* pointers to 3 tasks */
    taskPtrArray[0] = &taskArray[2]; /* pointer to task 2 */
    taskPtrArray[1] = &taskArray[6]; /* pointer to task 6 */
    taskPtrArray[2] = &taskArray[9]; /* pointer to task 9 */

    struct Processor * p1 = create_processor(1, taskPtrArray);

    printf("p1->id = %d \n", p1->id);
    p1->task[2]->WCET = 999;
    printf("p1->task[2]->WCET = %d \n", taskArray[9].WCET);
}

The output is:输出是:

p1->id = 1 p1->id = 1

p1->task[2]->WCET = 999 p1->任务[2]->WCET = 999


As "Some programmer dude" said.正如“一些程序员老兄”所说。 The function assing tasks with:功能分配任务:

pr->task = task;

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

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