0

所以我试图传递一个包含多个变量的结构,包括另一个处理位图信息的结构。但是,我的代码在某处失败,因为它在取消引用指向结构中包含的信息的指针时吐出错误“取消引用指向不完整类型的指针”。我知道这里有很多问题要处理这个问题,但我试图实施那里所说的并且失败了。

这是来自 main() 的相关代码,包括有关初始化的编辑:

pthread_t threads[thread_num];
pthread_attr_t attr;
int rc;
void *status;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

// Create a bitmap of the appropriate size.
struct bitmap *bitm = bitmap_create(image_width,image_height);

struct thread_args *arguments = (struct thread_args*) malloc(sizeof(struct thread_args));
arguments->bm = bitm;
arguments->xmin = xcenter-scale;
arguments->xmax = xcenter+scale;
arguments->ymin = ycenter-scale;
arguments->ymax = ycenter+scale;
arguments->max = max;
// Compute the Mandelbrot image
for(int i=0;i<thread_num;i++){
    arguments->thread_id = i;
if(pthread_create(&threads[i], NULL, compute_image, (void *)arguments)<0){
   printf("ERROR; return code from pthread_create() is %d\n", rc);
}
 }

 pthread_attr_destroy(&attr);
 for(int t=0; t<thread_num; t++) {
    rc = pthread_join(threads[t], &status);
    if (rc) {
        printf("ERROR; return code from pthread_join() is %d\n", rc);
     exit(-1);
     }
 }

以下是作为 pthread_create 参数传递的函数的相关代码:

void* compute_image(void *threadargs ){
   int i,j;
   struct thread_data *my_data = (struct thread_args*) malloc(sizeof(struct thread_args));

   my_data = (struct thread_data *) threadargs;
   int width = bitmap_width(my_data->bm);
   int height = bitmap_height(my_data->bm);
   int threads = my_data->threads;
int thread_id = my_data->thread_id;
double xmin = my_data->xmin;
double xmax = my_data->xmax;
double ymin = my_data->ymin;
double ymax = my_data->ymax;
int max = my_data->max;


// For every pixel in the image...

for(j=height/threads*thread_id;j<height/threads*(thread_id+1);j++) {

    for(i=0;i<width;i++) {

        // Determine the point in x,y space for that pixel.
        double x = xmin + i*(xmax-xmin)/width;
        double y = ymin + j*(ymax-ymin)/height;

        // Compute the iterations at that point.
        int iters = iterations_at_point(x,y,max);

        // Set the pixel in the bitmap.
        bitmap_set(my_data->bm,i,j,iters);
    }
}

}

这是结构:

struct thread_args{
    int thread_id;
    int threads;
    struct bitmap *bm;
    double xmin;
    double xmax;
    double ymin;
    double ymax;
    int max;
};
4

2 回答 2

2

我发现您的代码有两个问题:

  1. 你定义struct thread_args,但在compute_image(),你使用struct thread_data。这些不一样,(我猜)你的意思是thread_args,不是thread_data。这很可能解释了你得到的编译错误。您可能会考虑struct image_data改为命名它;这可能不太可能引起混乱。
  2. my_data您使用d 内存进行初始化malloc(),然后立即从 cast of 重新分配它threadargs。这是一个内存泄漏,没有充分的理由。只需删除malloc初始化。
于 2011-03-27T05:04:20.707 回答
0

这是您将 的定义放在哪里的问题struct thread_args。当编译器只有可用的原型时,它对结构成员一无所知。您需要将结构定义保留在包含的头文件中,或者在包含定义的 src 文件中创建访问器函数。

于 2011-03-23T15:02:36.470 回答