[英]Invalid conversion of int to unsigned int
此代碼用於分配。 但是,計算pi carte函數存在錯誤。 從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);
}
我做了一些調試,現在看看錯誤可能在哪里。 現在請發布主要函數,其中將調用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 );
}
int
rand_r(unsigned *ctx);
如您所見,它需要一個unsigned
指針-如果將其傳遞給int
並且在編譯器中打開了適當的警告/錯誤(而且看來確實如此),那么您將得到該錯誤。 更改seed
類型,您將被設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.