Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.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++ int到无符号int的转换无效_C++_Montecarlo_Pi - Fatal编程技术网

C++ int到无符号int的转换无效

C++ int到无符号int的转换无效,c++,montecarlo,pi,C++,Montecarlo,Pi,此代码用于分配。然而,即时计算pi蒙特卡罗函数中存在错误。从int到unsigned int的转换错误统计无效。我不确定这是否是有意的,但我无法纠正这个问题。此函数是线程示例的一部分,以防您感到疑惑。任何建议都将不胜感激 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 = (in

此代码用于分配。然而,即时计算pi蒙特卡罗函数中存在错误。从int到unsigned int的转换错误统计无效。我不确定这是否是有意的,但我无法纠正这个问题。此函数是线程示例的一部分,以防您感到疑惑。任何建议都将不胜感激

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;//*error is these lines
  rand_no_y = (double) (rand_r( &seed ))/(double)RAND_MAX;//*error is these lines
  if(((rand_no_x - 0.5) * (rand_no_x - 0.5) +
  (rand_no_y - 0.5) * (rand_no_y - 0.5)) < 0.25)//*error 
local_hits++;
  seed *= ii;
 }

*hit_pointer = local_hits;
 pthread_exit(0); 
 }
void*compute\u pi(void*s)
{
int种子;
int ii;
int*hit_指针;
int本地点击率;
双随机数;
双随机数;
点击指针=(int*)s;
种子=*命中指针;
本地点击率=0;
对于(ii=0;ii
我做了一些调试,现在看到了错误的位置。现在我将发布调用compute pi的主函数。我相信这是将参数解析为样本点和线程数的地方。当我运行该程序时,它会要求输入,之后它会因分段错误而失败。有什么建议吗

  #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_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 );

  printf( "Enter number of sample points: " );
  scanf( "%d", &sample_points );
  printf( "Enter number of threads: " );
  scanf( "%d%", &num_threads );

  /* 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]);


  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 );
   }
#包括
#包括
#包括
#定义最大线程数512
void*计算π(void*);
int样本点;
int总点击数;
总未命中次数;
int命中率[最大线程数];
每个线程的int样本点;
int num_线程;
int main(int argc,char*argv[])
{
/*局部变量*/
int ii;
内部检索;
pthread_t p_线程[最大线程];
pthread_attr_t attr;
双计算_-pi;
/*初始化局部变量*/
retval=0;
pthread_attr_init(&attr);
pthread_attr_setscope(&attr,pthread_SCOPE_SYSTEM);
printf(“输入采样点数:”);
扫描频率(“%d”和采样点);
printf(“输入线程数:”);
scanf(“%d%”,&num_线程);
/*将命令行参数解析为示例点和线程数*/
/*这里没有错误检查*/
样本点=atoi(argv[1]);
num_threads=atoi(argv[2]);
总点击次数=0;
每个线程的样本点数=样本点数/num线程;
对于(ii=0;ii来自:


如您所见,它需要一个
无符号的
指针-如果您向它传递一个
int
,并且您的编译器中打开了相应的警告/错误(看起来是这样的),您将得到该错误。更改
种子的类型,您将被设置。

是否有意,原始的“修复”似乎源于声明
seed
以匹配所提供的
void*
参数
命中指针的类型。此“匹配”您是正确的,确实指定使用
unsigned int*
作为其单个参数。请仔细阅读错误消息,不要对其进行解释。错误不是将
int
转换为
unsigned
;这没关系。错误是将
int*
转换为
unsigned*
无效,这是相当不同的。同时更改
命中指针的类型。哦,和
本地命中
。但是它们应该是int数据类型,对吗?因为它们处理的是整数。命中指针由为采样点和种子输入的数字分配一个int。好的,我这样做了“unsigned int seed;”现在没有出现错误,只是对pthread_create和pthread_join的引用未定义。我猜在main中创建和连接线程的过程中出现了错误?不,这意味着您没有链接线程库。请尝试将
-lpthread
添加到链接步骤。是的,可能。您可能需要
-lpthread
虽然结束了,而不是在中间,你试过了吗?
int
rand_r(unsigned *ctx);