简体   繁体   中英

How to pass a struct with an matrix as pthread argument?

I have the following struct:

typedef struct {
  int row;
  int** matrix;
} values ;

To fill the struct matrix I tried the following code:

values **v = (values **)malloc(x * sizeof(values *));
for (int z = 0; z < y; ++z)
     [z] = (values *)malloc(y * sizeof(values));

Where x is the number of rows and y columns.

How can I populate the arguments ( row and matrix ) of struct and pass as parameter to a function called by pthread? Something similar to...

pthread_create(&thread1, NULL, somaLinha, v);

When you allocate the space for the struct, C is actually going to allocate the space for an integer plus the space for the pointer (which is 4+8 bytes)

You need to allocate the space for the struct, and then allocate for the matrix

values *v = (values *) malloc(sizeof(values));
v->matrix = (int **) malloc(y * sizeof(int *));
for (int z = 0; z < y; ++z)
    v->matrix[z] = (int *) malloc(y * sizeof(int));

And then you create the thread

pthread_create(&thread1, NULL, somaLinha, v);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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