C 计算数组大小以使用线程计算平均值
我有一个巨大的头文件C 计算数组大小以使用线程计算平均值,c,multithreading,size,average,C,Multithreading,Size,Average,我有一个巨大的头文件average.h,其中包含4个数组。我想通过使用线程分别计算4个数组的平均值来计算头文件的平均值。 我在运行时遇到了一个分段错误,所以我想我如何计算数组的长度是有问题的。有人告诉我,不可能计算一个浮点**的大小,那么我该怎么做呢 这是我的密码: /* * File: main.c * Author: thomasvanhelden * * Created on June 15, 2014, 5:34 AM */ #include <stdio.h>
average.h
,其中包含4个数组。我想通过使用线程分别计算4个数组的平均值来计算头文件的平均值。
我在运行时遇到了一个分段错误,所以我想我如何计算数组的长度是有问题的。有人告诉我,不可能计算一个浮点**
的大小,那么我该怎么做呢
这是我的密码:
/*
* File: main.c
* Author: thomasvanhelden
*
* Created on June 15, 2014, 5:34 AM
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "average.h"
void* calc(void* param) {
float** array = (float**) param;
int size = sizeof(*array) / sizeof(float*);
int i;
float sum = 0;
float* average;
for (i = 0; i < size; i++) {
sum += *array[i];
}
*average = sum/size;
return average;
}
/*
*
*/
int main(int argc, char** argv) {
pthread_t t1, t2, t3, t4; // thread handlers
int res1, res2, res3, res4; // results of creating/joining the threads
void *avg1, *avg2, *avg3, *avg4; // results of the threads as void*
float *result1, *result2, *result3, *result4; // results of the threads as flaot*
// create the threads
res1 = pthread_create(&t1, NULL, calc, a1);
res2 = pthread_create(&t2, NULL, calc, a2);
res3 = pthread_create(&t3, NULL, calc, a3);
res4 = pthread_create(&t4, NULL, calc, a4);
// check for errors creating the threads
if (res1 || res2 || res3 || res4) {
printf("Something went wrong creating the threads!\n");
return 1;
}
// wait for the threads to finish and get the result
res1 = pthread_join(t1, &avg1);
res2 = pthread_join(t2, &avg2);
res3 = pthread_join(t3, &avg3);
res4 = pthread_join(t4, &avg4);
// check for errors joining the threads
if (res1 || res2 || res3 || res4) {
printf("Something went wrong joining the threads!\n");
return 1;
}
// void* to float*
result1 = (float*) avg1;
result2 = (float*) avg2;
result3 = (float*) avg3;
result4 = (float*) avg4;
// print the result, should be
printf("The average is: %f", (*result1 + *result2 + *result3 + *result4));
return (EXIT_SUCCESS);
}
/*
*文件:main.c
*作者:thomasvanhelden
*
*创建于2014年6月15日上午5:34
*/
#包括
#包括
#包括
#包括“平均.h”
无效*计算(无效*参数){
浮点**数组=(浮点**)参数;
int size=sizeof(*数组)/sizeof(浮点*);
int i;
浮点数和=0;
浮动*平均值;
对于(i=0;i
正如您所说,从指针检索数组的大小是不可能的。您必须将参数打包到结构中(您的float**
,加上数组的大小和任何其他相关信息),并将指向此结构的指针传递到pthread\u create()
请注意,辅助函数必须返回指针,因此需要分配内存。如果您希望避免动态分配,下面是一个将参数struct也作为返回值重用的模式:
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#define ARRAY_COUNT(arr) (sizeof (arr) / sizeof *(arr))
typedef union {
struct { // Function parameters
float *array;
size_t size;
};
struct { // Function return value
float result;
};
} arrayAverageParam_u;
void *arrayAverage(void *param) {
arrayAverageParam_u *_param = param;
// From now on we can use _param to access the struct
int i;
float avg = 0.0f;
for(i = 0; i < _param->size; ++i)
avg += _param->array[i];
if(i)
avg /= i;
// Store the result, overwriting the parameters
_param->result = avg;
return NULL;
}
main()
{
float array[] = {1.0f, 2.0f, 3.0f, 4.0f};
// Fill the struct with parameters
arrayAverageParam_u param = {
.array = array,
.size = ARRAY_COUNT(array),
};
pthread_t thread;
pthread_create(&thread, NULL, arrayAverage, ¶m);
pthread_join(thread, NULL);
// Retrieve the result from the struct
printf("The average is %g\n", param.result);
return 0;
}
#包括
#包括
#包括
#定义数组计数(arr)(sizeof(arr)/sizeof*(arr))
typedef联合{
结构{//函数参数
浮点*数组;
大小;
};
结构{//函数返回值
浮动结果;
};
}阵列平均参数;
无效*阵列平均值(无效*参数){
arrayAverageParam_*_param=param;
//从现在起,我们可以使用_param访问结构
int i;
浮动平均值=0.0f;
对于(i=0;i<_参数->大小;++i)
平均值+=_参数->数组[i];
如果(i)
平均值/=i;
//存储结果,覆盖参数
_参数->结果=平均值;
返回NULL;
}
main()
{
浮点数组[]={1.0f,2.0f,3.0f,4.0f};
//用参数填充结构
arrayAverageParam_param={
.array=数组,
.size=数组\数组计数(数组),
};
pthread\u t线程;
pthread_create(&thread,NULL,arrayAverage,¶m);
pthread_join(线程,NULL);
//从结构中检索结果
printf(“平均值为%g\n”,参数结果);
返回0;
}
显示数组的声明。这是否有效?我相信下面的语句会给出1,因为在32位平台上指针的大小总是4字节:int size=sizeof(*array)/sizeof(float*);好主意!我想把数组的大小硬编码到for循环中,但是你的听起来好多了。谢谢如果适合这个问题,别忘了接受这个答案:)对不起,我现在做了。我想我必须等几个小时才能接受答案…:/没问题,很高兴能帮上忙:)