简体   繁体   中英

Invalid conversion of int to unsigned int

This code is given for the assignment. However, off the bat there is an error in the compute pi monte carlo function. the error stats invalid of conversion from int to unsigned int. Im not sure if this was intended, but i haven't been able to correct this issue. This function is part of threading example in case you were wondering. Any suggestions is most appreciated

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); 
 }

i did some debugging and now see where the error may be located. ill post now the main function where compute pi is called. I believe it is where arguments are parsed into sample points and number of threads. When i run the program it asks for input afterwards it fails with segementation fault. Any suggestions?

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

From the rand_r(3) man page :

int
rand_r(unsigned *ctx);

As you can see, it takes an unsigned pointer - if you pass it an int and you have the appropriate warnings/errors turned on in your compiler (and it seems you do), you'll get that error. Change the type of seed and you'll be set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM