2
struct instruction {
   int value;
};

int n; // Number of instructions

struct instruction **instructions; //Required to use ** and dynamic array

Say I want to store n number of instructions and store a value in each instructions. How would I do that with the **instructions?? So I want to be able to call a value from a specific instruction later.

Many Thanks

So far I tried these, a few scanfs and dynamic array creating. So it takes number of counters, then takes number of thread (pthreads), then takes number of instructions within each thread. I am trying to find a way to store the instructions in each thread. ** structure is given

int
main(void) {

        scanf(" %d", &ncounters); //Ask for number of counters

        int i;

        if(counters = malloc(sizeof(struct counter)*ncounters)){
          for( i=0; i < ncounters ;i++){
            counters[i].counter = 0;
          }
        }

        scanf(" %d", &nthreads); //Ask for number of threads

        if(ninstructions = (int*) malloc(nthreads*sizeof(int))){
          for( i=0; i < nthreads ;i++){
            ninstructions[i] = 0;
          }
        }

        for(i=0; i < nthreads ;i++){

          scanf(" %d", &ninstructions[i]);  //Ask for number of instructions within threads[i]
         // Things got messy from here ...  
          instructions = malloc(sizeof(struct instruction*)*ninstructions[i]);

          for(int j=0; j < ninstructions[j] ;j++){
            instructions[j] = malloc(sizeof(struct instruction));
            int x;
            printf("Enter rep for thread %d inst %d.\n",i+1 ,j+1);
            scanf(" %d", &x);
            instructions[i][j].repetitions = x;
          }

        }

        printf(" Instruction x: %d.\n", instructions[0][0].repetitions);

//=============================================================================
// Just testing with printf
        printf("Number of counters: %d.\n", ncounters);
        printf("Number of threads: %d.\n", nthreads);

        for(i=0; i < nthreads; i++){
          printf("Thread %d has %d instructions.\n", i+1, ninstructions[i]);
        }

//=============================================================================

        free(instructions);
        free(ninstructions);
        free(counters);
    return 0;
}

I finally got some progress, but getting segmentation fault at the instruction storing part.

struct counter *counters;

struct instruction{
  struct counter *counter;
  int repetitions;
  void (*work_fn)(long long*);
};

for(i=0; i < nthreads ;i++){

  scanf(" %d", &ninstructions[i]);  //Ask for number of instructions within threads[i]

  instructions = malloc(nthreads*sizeof(struct instruction *));

  instructions[i] = malloc(ninstructions[i]*sizeof(struct instruction));
  for(int j=0;j < ninstructions[i] ;j++){
    int Srepetition;
    char Sfunction;
    int Scounter;

    scanf(" %d %c %d", &Scounter, &Sfunction, &Srepetition);

    //Problem seems to be here "Segmentation fault" ..............

    instructions[i][j].repetitions = Srepetition;
    instructions[i][j].counter = (counters+Scounter);

    if(Sfunction == 'I'){
      instructions[i][j].work_fn(&increment);
    }else if(Sfunction == 'D'){
      instructions[i][j].work_fn(&decrement);
    }else if(Sfunction == '2'){
      instructions[i][j].work_fn(&mult2);
    }else{
      printf("error\n");
    }

    printf("Thread: %d Instruction: %d.\n", i+1, j+1);
    }
}
3
  • 3
    Homework? (extra characters for stupid stupid stupid STUPID min comment length) Commented Apr 28, 2011 at 20:36
  • Why this is required to be double pointer? I don't think it's necessary here. Can you be more specific? And can you tell what have toy tried (: Commented Apr 28, 2011 at 20:39
  • Sounds like homework (or an exercise from a book). If so, you should tag it [homework], so we know what kind of answer to give you. Commented Apr 28, 2011 at 20:41

1 Answer 1

2

With just one * is enough to create a dynamic array, with ** you would be creating a matrix.

struct instruction *instructions = malloc(n * sizeof(struct instruction));

/* setting some random values */
for (int i=0;i<n;i++)
     instructions[i]->value = i;

/* accessing values */
for (int i=0;i<n;i++)
     printf("instruction %d value %d\n",i,instructions[i]->value);

/* don't forget to free */
free(instructions);

Please look for references about dynamic arrays in C to investigate more. For instance this one

edit

... if you really need matrix this is the equivalent code:

int n,z; // for number cols and rows

struct instruction **instructions = malloc(n * sizeof(struct instruction *));

/* setting some random values */
for (int i=0;i<n;i++) {
     instructions[i] = malloc(z * sizeof(struct instruction));
     for (int j=0;j<z;j++) 
         instructions[i][j]->value = i * j;
}
/* accessing values */
for (int i=0;i<n;i++) {
     for (int j=0;j<z;j++) 
          printf("instruction %d,%d value %d\n",i,j,instructions[i][j]->value);
}

/* don't forget to free */ 
for (int i=0;i<n;i++)
     free(instructions[i]);
free(instructions);
Sign up to request clarification or add additional context in comments.

4 Comments

@Jono see the new edition with the matrix code. I hope it helps.
Thank you, i understand it alot more now. Just one last question, should i scanf and store the instructions in main or in a thread function that i will call via pthread_create. From what I understand so far, i can either store it in ** and call it in a thread function(from pthread_create) or just call the thread function(from pthread_create) then store the instruction.
Having many threads reading from stdin is not a good idea. I would do it in the main. To be honest I don't understand the purpose of threads in your program. Maybe you should post a different question for that.
Could you please help me with the updated ver. ? I am having trouble passing scanf value into the 2D array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.