Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c结构和指针分配pthread_C_Pointers_Segmentation Fault_Structure - Fatal编程技术网

c结构和指针分配pthread

c结构和指针分配pthread,c,pointers,segmentation-fault,structure,C,Pointers,Segmentation Fault,Structure,我正在做一项作业,我必须用pthreadsC处理一些图像 我有以下结构: typedef struct { int type; int width; int height; int max_value; int *input; }image; 及 我还有一个函数,其中我尝试实例化一个结构_b数组,并将给定结构_a的参数分配给这些数组中的每一个 void resize(image *in, image * out) { int i; pth

我正在做一项作业,我必须用
pthreads
C处理一些图像

我有以下结构:

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

我还有一个函数,其中我尝试实例化一个结构_b数组,并将给定结构_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]));
    }
}
void调整大小(图像*in,图像*out){
int i;
pthread_t tid[num_threads];
ptf_参数*参数[num_线程];
参数[0]->in->input=(int*)malloc(in->width*in->height*sizeof(int));
参数[0]->in=in;//此处
printf(“第一个线程应该有id=%d。在(%d)\n)的图像中,参数[0]->id,参数[0]->In->width);//这里
对于(i=0;i
1) 我不太了解结构,我需要有人向我解释如何将输入/输出图像传递到我的ptf_参数结构,以便将其传递到pthreads函数

2) 是否需要为映像结构分配内存

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


谢谢

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

ptf_arguments *arguments[num_threads];
arguments[0]->in->undefined behavior
在取消引用之前,您需要将
中的
指向有效的
图像结构。最简单的方法可能是将标记的行移动到上一行的上方

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


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

结构基本上是一个不能更改其子部分的对象。
它主要类似于一个Javascript变量。使用点
varname访问其子部分。如果变量是普通变量,则使用箭头
varname->subsection
(如果是指针)

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

每个子部分必须根据其类型进行初始化。因此,指针子部分必须与任何指针完全相同,或者使用现有指针定义

void调整大小(图像*in,图像*out){
pthread_t tid[num_threads];
ptf_参数*参数[num_线程];
//参数[0]->in->input=malloc(in->width*in->height*sizeof(int));
//前一行是无用的(也使用未初始化的项)。您已经有了完整的指针。
参数[0]->in=in;//此处
printf(“第一个线程应该有id=%d。在(%d)\n)的图像中,参数[0]->id,参数[0]->In->width);//这里
for(int i=0;i
1) 我不太了解结构,我需要有人向我解释如何将输入/输出图像传递到我的ptf_参数结构,以便将其传递到pthreads函数

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

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

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

2) 是否需要为映像结构分配内存

对。这也是代码不起作用的另一个原因<代码>ptf_参数*参数[num_线程]
只是一个指针数组,没有为实际数据分配内存

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

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

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