【问题标题】:Converting an array of struct pollfd to a pointer of pointer of struct pollfd将 struct pollfd 的数组转换为 struct pollfd 的指针的指针
【发布时间】:2021-04-16 17:46:12
【问题描述】:

假设我有一个 struct pollfds 数组

 int Pipes = 2;
 struct pollfd pollFD[nArguments][nPipes];

nArguments 是一个在 main 内部确定的变量,我无法在运行前知道它的值,我需要将这个 pollfd 数组放在 struct 中,以将其作为参数传递给thread.

如何转换成

struct pollfd **pollFDConverted;

编辑:我在这个链接找到了解决方案:Malloc a 2D array in C

【问题讨论】:

  • 将其包裹在一个结构struct Mypollfd { struct pollfd **pollFDConverted; size_t nArguments; size_t nPipes;}中,然后将struct Mypollfd*传递给线程?

标签: arrays c struct


【解决方案1】:

可以做类似this:

#include <stdio.h>
#include <pthread.h>
#include <poll.h>
#include <stdlib.h>

struct pollFDWrapper {
    struct pollfd ** arr;
    size_t nRows;
    size_t nCols;
};

static void* thread_handler(void *arg)
{
    struct pollFDWrapper * wrapper = arg;
    for(size_t i = 0; i < wrapper->nRows; ++i)
        for(size_t j = 0; j < wrapper->nCols; ++j) {
            (void) wrapper->arr[i][j];
        }
            
    return NULL;
}

int main(void)
{
    struct pollFDWrapper * arg = malloc(sizeof *arg);
    //Dimensions can be obtained as required, just for demonstration
    arg->nRows = 3;
    arg->nCols = 5;
    arg->arr = calloc(sizeof arg->arr[0], arg->nRows); //create array of pointers
    for(size_t i = 0; i < arg->nRows; ++i) {
        arg->arr[i] = calloc(sizeof arg->arr[i][0], arg->nCols); //crate array
    }

    pthread_t thread;

    if (pthread_create(&thread, NULL, thread_handler, arg) != 0)
    {
        perror("pthread_create");
        exit(EXIT_FAILURE);
    }
    if (pthread_join(thread, NULL) != 0)
    {
        perror("pthread_join");
        exit(EXIT_FAILURE);
    }
    for(size_t i = 0; i < arg->nRows; ++i) {
        free(arg->arr[i]); //free array
    }
    free(arg->arr); //free ptr to array
    free(arg); //free the encapsualting struct
    return 0;
}

struct pollFDWrapper 封装了操作struct pollfd ** arr 所需的信息,包括二维数组的维度,并且可以像普通指针一样传递,甚至传递给线程。

【讨论】:

  • @DavidRanieri 感谢您的赞赏!!
【解决方案2】:

如何转换成

struct pollfd **pollFDConverted;

你不想这样做,编译器需要知道最后一维的元素数量,另一方面,线程处理程序使用指向void的指针作为参数,你可以传递任何你想要的它(指向函数的指针除外)。

只需转换为正确的类型(在这种情况下,我通过指向 n 个数组的指针读取ints 的二维数组ints,您可以将其调整为您的struct):

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

enum {rows = 2, cols = 3};

static void *thread_handler(void *arg)
{
    int (*arr)[cols] = arg;

    for (int row = 0; row < rows; row++)
    {
        for (int col = 0; col < cols; col++)
        {
            printf("%d ", arr[row][col]);
        }
        printf("\n");
    }
    return NULL;
}

int main(void)
{
    int arr[rows][cols] = {{1, 2, 3}, {4, 5, 6}};
    pthread_t thread;

    if (pthread_create(&thread, NULL, thread_handler, arr) != 0)
    {
        perror("pthread_create");
        exit(EXIT_FAILURE);
    }
    if (pthread_join(thread, NULL) != 0)
    {
        perror("pthread_join");
        exit(EXIT_FAILURE);
    }
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多