gpt4 book ai didi

c - 从 ‘int*’ 到 ‘unsigned int*’ 的无效转换 [-fpermissive]

转载 作者:行者123 更新时间:2023-11-30 16:21:08 25 4
gpt4 key购买 nike

我的老师给了我这段应该可以编译的代码。它应该通过使用线程来计算 PI。然后我们应该对程序进行计时和/或向代码添加适当的计时系统调用。运行不同的线程并创建一个比较时间和线程数量的图...问题是,我不知道如何解决这个问题。我从未处理过这样的事情,也没有自己构建代码,所以我对如何修复它感到困惑。

编译器说问题来自:

rand_no_y = (double) (rand_r( &seed ))/(double)RAND_MAX;
^

rand_no_x = (double) (rand_r( &seed ))/(double)RAND_MAX;

完整代码如下:

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


#define MAX_THREADS 512

void *compute_pi( void * );


//int sample_points;
int total_hits;
int total_misses;
int hits[ MAX_THREADS ];
int sample_points;
int sample_points_per_thread;
int num_threads;



int main( int argc, char *argv[] )
{
/* local variables */
int ii;
int retval;
pthread_t p_threads[MAX_THREADS];
pthread_attr_t attr;
double computed_pi;

/* initialize local variables */
retval = 0;


pthread_attr_init( &attr );
pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );

/* parse command line arguments into sample points and number of threads */
/* there is no error checking here!!!!! */
sample_points = atoi(argv[1]);
num_threads = atoi(argv[2]);

/* uncomment this block if you want interactive input!!!! */
/* if so...comment out the two statements above */
/*
printf( "Enter number of sample points: " );
scanf( "%d", &sample_points );
printf( "Enter number of threads: " );
scanf( "%d%", &num_threads );
*/

total_hits = 0;
sample_points_per_thread = sample_points / num_threads;

for( ii=0; ii<num_threads; ii++ )
{
hits[ii] = ii;
pthread_create( &p_threads[ ii ], &attr, compute_pi, (void *) &hits[ii] );
}

for( ii=0; ii<num_threads; ii++ )
{
pthread_join( p_threads[ ii ], NULL );
total_hits += hits[ ii ];
}

computed_pi = 4.0 * (double) total_hits / ((double) (sample_points));


printf( "Computed PI = %lf\n", computed_pi );


/* return to calling environment */
return( retval );
}


void *compute_pi( void *s )
{
int seed;
int ii;
int *hit_pointer;
int local_hits;
double rand_no_x;
double rand_no_y;

hit_pointer = (int *) s;
seed = *hit_pointer;
local_hits = 0;

for( ii=0; ii < sample_points_per_thread; ii++ )
{
rand_no_x = (double) (rand_r( &seed ))/(double)RAND_MAX;

rand_no_y = (double) (rand_r( &seed ))/(double)RAND_MAX;

if(((rand_no_x - 0.5) * (rand_no_x - 0.5) +
(rand_no_y - 0.5) * (rand_no_y - 0.5)) < 0.25)

local_hits++;

seed *= ii;
}

*hit_pointer = local_hits;
pthread_exit(0);
}

最佳答案

对于int seed&seed是一个int *

rand_r(unsigned int *seedp)需要一个无符号*

seed、ii 和其他使用 unsigned

关于c - 从 ‘int*’ 到 ‘unsigned int*’ 的无效转换 [-fpermissive],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54988994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com