1

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

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; but it returns ERROR Segmentation fault (core dumped)

Help please

13
  • 4
    Have you tried the (pretty obvious) pr->task = task;? Commented Mar 1, 2019 at 17:34
  • 2
    Also note that you only allocate memory for one Processor structure. And remember that array indexes are zero based. Commented Mar 1, 2019 at 17:35
  • 1
    help it's urgent please Commented Mar 1, 2019 at 18:05
  • 1
    @ikram Maybe to you it's urgent but you're asking complete strangers for free help. Commented Mar 1, 2019 at 18:30
  • 2
    Your error is probably in the code you haven't shown us. Please update your question with a minimal reproducible example that others can compile and run that reproduces your problem. Commented Mar 1, 2019 at 18:32

2 Answers 2

1

When you do

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

you've now initialized an empty Processor struct. 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 }, i.e. it just contains a null pointer for the task member. That's probably why you get Segmentation fault, which is the type of error that occurs when your program accesses uninitialized memory.

You'll also have to malloc an array of Task* (task pointers) for however many tasks each processor has to have. E.g.

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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:

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->task[2]->WCET = 999


As "Some programmer dude" said. The function assing tasks with:

pr->task = task;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.