【问题标题】:c structures and pointer allocation pthreadc结构和指针分配pthread
【发布时间】:2018-10-30 13:56:42
【问题描述】:

我正在做一个任务,我必须用pthreads C 处理一些图像。

我有以下结构:

typedef struct {
    int type;
    int width;
    int height;
    int max_value;
    int *input;
}image;

typedef struct {
    int id; // thread id
    image *in; //input image
}ptf_arguments;

我还有一个函数,我尝试实例化一个 structure_b 数组,并将给定 structure_a 的参数分配给每个函数

void resize(image *in, image * out) {
    int i;
    pthread_t tid[num_threads];
    ptf_arguments *arguments[num_threads];
    arguments[0]->in->input = (int *)malloc(in->width * in->height * sizeof(int));
    arguments[0]->in = in; // HERE

    printf("First thread should have id = %d. In image of (%d)\n", arguments[0]->id, arguments[0]->in->width); //here
    for(i = 0 ; i < num_threads; i++) {
        pthread_create(&(tid[i]), NULL, resize_thread_function, &(arguments[i]));
    }
}

1) 我不太了解结构,我需要有人向我解释如何将输入/输出图像传递给我的 ptf_arguments 结构,以便我可以将它传递给 pthreads 函数。

2) 我需要为图像结构分配内存吗?

3) 是否需要为 ptf_arguments 结构的图像结构内的int 数组分配内存?

谢谢

【问题讨论】:

    标签: c pointers segmentation-fault structure


    【解决方案1】:

    当我阅读代码时,我明白了

    ptf_arguments *arguments[num_threads];
    arguments[0]->in->undefined behavior
    

    在取消引用之前,您需要将 in 指向有效的 image 结构。最简单的方法可能是将标记为//HERE 的行移到上一行的上方。

    是的,您还需要分配一个int 数组并将image 结构的input 成员指向该内存。不用说,您需要为每个 ptf_arguments 对象进行初始化和分配,而不仅仅是第一个。

    我认为您传递单个 ptf_arguments 对象地址的语法很好。

    【讨论】:

      【解决方案2】:

      结构基本上是一个您无法更改其子部分的对象。
      它主要类似于 Javascript 变量。如果变量是普通变量,则使用点 varname.subpartname 访问其子部分,如果是指针,则使用箭头 varname-&gt;subpart

      结构是变量类型。您不必为它们分配内存(除非使用指针...)

      每个子部分必须根据它们的类型进行初始化。因此,指针子部分必须与任何指针完全一样malloc'ed,或使用现有指针定义。

      void resize(image *in, image * out) {
          pthread_t tid[num_threads];
          ptf_arguments *arguments[num_threads];
      //    arguments[0]->in->input = malloc(in->width * in->height * sizeof(int));
      //The preceding line is useless (and also using an uninitialized item). You already have a complete pointer.
          arguments[0]->in = in; // HERE
      
          printf("First thread should have id = %d. In image of (%d)\n", arguments[0]->id, arguments[0]->in->width); //here
          for(int i = 0 ; i < num_threads; i++) {//Since you only use int i here, you can declare it in the for condition
              pthread_create(&(tid[i]), NULL, resize_thread_function, &(arguments[i]));
          }
      }
      

      【讨论】:

        【解决方案3】:

        1) 我不太了解结构,我需要有人向我解释如何将输入/输出图像传递给我的 ptf_arguments 结构,以便我可以将它传递给 pthreads 函数。

        如果每个线程需要 1 个结构,请使用 arg 参数按地址将其传递给 pthread_create。就像您已经尝试过的那样:&amp;(arguments[i])。不过,您可能只想做arguments[i],因为这是一个指针。您实际上传递的不是地址分配的数据,而是本地指针的地址,这是错误且不正确的。

        重要提示:永远不要将指向局部变量的指针传递给线程!需要使用静态存储持续时间或通过动态分配来分配变量。

        如果您没有“很好地理解结构”,您可能应该学习它们并更多地练习使用它们,然后再继续学习诸如多线程之类的高级主题。一般来说,你不能通过反复试验来编程,你实际上需要知道输入的每一行是做什么的。没有“冒险”。

        2) 我需要为图像结构分配内存吗?

        是的。这是您的代码不起作用的另一个原因。 ptf_arguments *arguments[num_threads]; 只是一个指针数组,没有为实际数据分配内存。

        3) 是否需要为 ptf_arguments 结构的 image 结构内的 int 数组分配内存?

        是的。

        还记得free使用完所有内存。一个名为“resize”的函数可能不应该分配资源,除非它首先释放以前使用的资源。

        【讨论】:

          猜你喜欢
          • 2014-02-23
          • 2013-03-02
          • 1970-01-01
          • 2021-04-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多