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

LeoVen
  • 632
  • 8
  • 18
ikram
  • 25
  • 6
  • 4
    Have you tried the (pretty obvious) `pr->task = task;`? – Some programmer dude Mar 01 '19 at 17:34
  • 2
    Also note that you only allocate memory for ***one*** `Processor` structure. And remember that array indexes are *zero* based. – Some programmer dude Mar 01 '19 at 17:35
  • thanks, yes i tried that but the same problm, it show : `request for member ‘task’ in something not a structure or union` – ikram Mar 01 '19 at 17:48
  • For each processor i allocate memory using `pr[1]=create_processor(1, NULL);` but i don't know if it is correcte i mean NULL – ikram Mar 01 '19 at 17:51
  • Please provide structure of Task – priojeet priyom Mar 01 '19 at 17:53
  • here is : `struct Task{ int id; int WCET;}` – ikram Mar 01 '19 at 17:55
  • 1
    help it's urgent please – ikram Mar 01 '19 at 18:05
  • 1
    @ikram Maybe to you it's urgent but you're asking complete strangers for free help. – Iguananaut Mar 01 '19 at 18:30
  • 2
    Your error is probably in the code you haven't shown us. Please update your question with a [mcve] that others can compile and run that reproduces your problem. – dbush Mar 01 '19 at 18:32
  • If you want better help, improve the question. I can't see any reason why @Someprogrammerdude 's suggestion wouldn't work, so it's time for you to post the [minimal complete verifiable example](https://stackoverflow.com/help/mcve) that gives that error. – Useless Mar 01 '19 at 18:32
  • See also many similar questions, such as https://stackoverflow.com/questions/1761809/nested-struct-variable-initialization – Iguananaut Mar 01 '19 at 18:38
  • Thanks every one ..That's OK i will rewrite my question Now.... but to resume i want just to know how to access the elements of a struct table (array struct) that is part of another array struct. and i will try it and show you the result. – ikram Mar 01 '19 at 18:41
  • And please take some time to read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](https://meta.stackoverflow.com/questions/326569/under-what-circumstances-may-i-add-urgent-or-other-similar-phrases-to-my-quest) – Some programmer dude Mar 01 '19 at 19:07

2 Answers2

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.

Iguananaut
  • 21,810
  • 5
  • 50
  • 63
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;