I keep getting a segfault in some code I am trying and I am unsure why. Please take a look below (I've pulled the required into a single window, the functions are actually within 3 different files):
#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;
}
I can get most of these parameters setup except for the particle_t* p
inside my bins struct. Below is how I am trying to accomplish that:
(I know setting p[0]
over and over isn't going to accomplish what I want, I was just trying to dumb it down and have it set over and over)
gdb gives me the following result:
(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];
For some more background, the particles are being initialized in another function and I am trying to see which particles fall in a particular bin's area and add the particles that do to that bin, storing them in an array. I don't think I am grasping the idea of pushing the particles into an array correctly... I hope the above gives you a good idea of what I'm trying to do without being too much extra.