尝试将指针复制到数组中

时间:2019-03-20 23:42:03

标签: c pointers

我一直在尝试一些代码中遇到段错误,但不确定为什么。请在下面查看(我将必需项放到一个窗口中,这些功能实际上位于3个不同的文件中):

#define density 0.0005 

const int NBINS = 10;

double size;

typedef struct 
{
   int x_start;
   int x_end;
   int y_start;
   int y_end;
   particle_t* p;
   bool left;
   bool right;
   bool up;
   bool down;
} bins;

typedef struct 
{
  double x;
  double y;
  double vx;
  double vy;
  double ax;
  double ay;
} particle_t;

int find_option( int argc, char **argv, const char *option )
{
    for( int i = 1; i < argc; i++ )
        if( strcmp( argv[i], option ) == 0 )
            return i;
    return -1;
}

int read_int( int argc, char **argv, const char *option, int default_value )
{
    int iplace = find_option( argc, argv, option );
    if( iplace >= 0 && iplace < argc-1 )
        return atoi( argv[iplace+1] );
    return default_value;
}

void set_size( int n ) {
    size = sqrt( density * n );
}

void init_particles( int n, particle_t *p ) {
    srand48( time( NULL ) );

    int sx = (int)ceil(sqrt((double)n));
    int sy = (n+sx-1)/sx;

    int *shuffle = malloc( n * sizeof(int) );
    for( int i = 0; i < n; i++ )
        shuffle[i] = i;

    for( int i = 0; i < n; i++ ) 
    {
        int j = lrand48()%(n-i);
        int k = shuffle[j];
        shuffle[j] = shuffle[n-i-1];

        p[i].x = size*(1.+(k%sx))/(1+sx);
        p[i].y = size*(1.+(k/sx))/(1+sy);

        p[i].vx = drand48()*2-1;
        p[i].vy = drand48()*2-1;
    }
    free( shuffle );
}

void create_bins(int n, int num_bins, bins* b) {
   int x = 0;
   int y = 0;

   for (int i = 0; i < num_bins; i++) {
      b[i].x_start = x; 
      b[i].x_end = x + NBINS; 
      x += NBINS;

      b[i].y_start = y; 
      b[i].y_end = y + NBINS; 
      y += NBINS;

      b[i].p = malloc(n * sizeof(particle_t));
   }
}

void add_to_bin (int n, int num_bins, bins* b, particle_t* p) {
   for (int i = 0; i < n; i++)
       for (int j = 0; j < num_bins; i++)
          if (p[i].x >= b[j].x_start && p[i].x < b[j].x_end)    // Check if particle is within bin's x boundaries
             if(p[i].y >= b[j].y_start && p[i].y < b[j].y_end) {  // Check if particle is within bin's y boundaries
                b[j].p[0] = p[i];
             }
}

int main( int argc, char **argv )
{
   if( find_option( argc, argv, "-h" ) >= 0 )
   {
      printf( "Options:\n" );
      printf( "-h to see this help\n" );
      printf( "-n <int> to set the number of particles\n" );
      printf( "-o <filename> to specify the output file name\n" );
      printf( "-s <filename> to specify a summary file name\n" );
      printf( "-no turns off all correctness checks and particle output\n");
      return 0;
   }

   int n = read_int( argc, argv, "-n", 1000 );

   int num_bins = n / NBINS;

   bins* b = malloc (num_bins * sizeof(bins));

   create_bins(n,num_bins,b);

   particle_t *particles = malloc( n * sizeof(particle_t) );
   set_size( n );
   init_particles( n, particles );

   add_to_bin(n,num_bins,b,particles);

   return 0;
}

除了bin结构中的particle_t* p外,我可以设置大多数这些参数。下面是我如何实现这一目标的方法:

(我知道一遍又一遍地设置p[0]并不能实现我想要的功能,我只是想把它弄糊涂,一遍又一遍地设置它)

gdb给我以下结果:

(gdb) run
Starting program: /workspace/particle/serial

Program received signal SIGSEGV, Segmentation fault.
0x0000000008001101 in add_to_bin (n=1000, num_bins=100, b=0x8415e70, p=<optimized out>) at bins.h:70
70                                      b[j].p[0] = p[i];

有关更多背景信息,正在另一个函数中初始化粒子,我试图查看哪些粒子落在特定容器的区域中,然后将要执行操作的粒子添加到该容器中,并将它们存储在数组中。我不认为我掌握了将粒子正确地推入数组的想法……我希望以上内容可以使您对我正在尝试做的事情有个好主意,而无需过多投入。

0 个答案:

没有答案