简体   繁体   English

如何通过矩阵作为pthread参数传递结构?

[英]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. 其中x是行数和y列数。

How can I populate the arguments ( row and matrix ) of struct and pass as parameter to a function called by pthread? 如何填充struct的参数( rowmatrix ),并将其作为参数传递给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) 当您为结构分配空间时,C实际上将为整数分配空间加上指针的空间(4 + 8字节)

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

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

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