简体   繁体   English

Pthreads将void *参数转换为int数组

[英]Pthreads cast void * argument into int array

I'm dealing with this code written in c++: 我正在处理用C ++编写的以下代码:

int *V;
V = new int[nfilas*ncols];
iret=pthread_create(&threadList[i], NULL, worker_function, (void*)(&V)[nfilas*ncols]);

that passes "V" (a int array) to this function: 将“ V”(一个int数组)传递给此函数:

    int *matrix=(int*)ptr;
    for( int r=0; r<nfilas; ++r ){
    for( int c=0; c<ncols; c++ ){
        printf("%d ", matrix[r*ncols+c]);
    }

My problem is that i can't convert that void pointer into an array of int elements. 我的问题是我无法将void指针转换为int元素数组。 How can i solve this? 我该如何解决? I've try many thing but I don't know what I'm doing wrong. 我已经尝试了很多事情,但是我不知道自己在做什么错。 Thank you in advance 先感谢您

You've got too much going on in your cast… 您的演员阵容有很多事情要做...

Spawn your thread with pthread_create(..., V); // V is already a pointer pthread_create(..., V); // V is already a pointer您的线程pthread_create(..., V); // V is already a pointer pthread_create(..., V); // V is already a pointer

You can safely cast from int * to void * and back... 您可以安全地从int *void *并返回...

int *V;
V = new int[nfilas*ncols];
iret = pthread_create(
    &threadList[i], NULL, worker_function, static_cast<void *>(V));

In your function... 在您的工作中...

int *matrix = static_cast<int *>(ptr);
for(int r = 0; r < nfilas; r++){
    for(int c = 0; c < ncols; c++){
        printf("%d ", matrix[r*ncols+c]);
    }
}

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

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