为结构赋值时访问冲突写入位置
Access violation writing location while giving value to a struct
我试图在循环中为二维结构赋值,但我不断收到访问冲突写入位置错误
我的代码是:
typedef struct {
int prtcls[10],numb;
} intpos;
int main(int argc, char** argv)
{
particle_t* particles;
intpos** points = new intpos*[(int)SCALE];
for (int i = 0; i < (int)SCALE; i++) {
intpos* row = new intpos[(int)SCALE - 1];
points[i] = row;
}
cudaMallocHost(&particles, sizeof(particle_t)*NUM_PARTICLES);
// Random initial positions / directions.
for (int i = 0; i < NUM_PARTICLES; i++)
{
particles[i].x =( (float)rand() / (float)RAND_MAX) * SCALE ;
particles[i].y = ((float)rand() / (float)RAND_MAX) * SCALE;
particles[i].phi = ((float)rand() / (float)RAND_MAX) * 2 * 3.14;
particles[i].flrx = floor(particles[i].x) ;
particles[i].flry = floor(particles[i].y) ;
int fx = particles[i].flrx;
int fy = particles[i].flry;
points[fx][fy].numb += 1;
int curn = points[fx][fy].numb;
points[fx][fy].prtcls[curn] = i;
}
完整的错误是:
0xC0000005:访问冲突写入位置 0x000001DEA3C7836C。
什么线路?如果您使用的是 GNU 工具,gdb 和堆栈跟踪会告诉您哪一行崩溃了。
我怀疑是以下几行之一:
particles[i].x =( (float)rand() / (float)RAND_MAX) * SCALE ;
points[fx][fy].numb += 1;
points[fx][fy].prtcls[curn] = i;
您绝对确定所有这些数组索引值都在范围内吗?
我试图在循环中为二维结构赋值,但我不断收到访问冲突写入位置错误 我的代码是:
typedef struct {
int prtcls[10],numb;
} intpos;
int main(int argc, char** argv)
{
particle_t* particles;
intpos** points = new intpos*[(int)SCALE];
for (int i = 0; i < (int)SCALE; i++) {
intpos* row = new intpos[(int)SCALE - 1];
points[i] = row;
}
cudaMallocHost(&particles, sizeof(particle_t)*NUM_PARTICLES);
// Random initial positions / directions.
for (int i = 0; i < NUM_PARTICLES; i++)
{
particles[i].x =( (float)rand() / (float)RAND_MAX) * SCALE ;
particles[i].y = ((float)rand() / (float)RAND_MAX) * SCALE;
particles[i].phi = ((float)rand() / (float)RAND_MAX) * 2 * 3.14;
particles[i].flrx = floor(particles[i].x) ;
particles[i].flry = floor(particles[i].y) ;
int fx = particles[i].flrx;
int fy = particles[i].flry;
points[fx][fy].numb += 1;
int curn = points[fx][fy].numb;
points[fx][fy].prtcls[curn] = i;
}
完整的错误是: 0xC0000005:访问冲突写入位置 0x000001DEA3C7836C。
什么线路?如果您使用的是 GNU 工具,gdb 和堆栈跟踪会告诉您哪一行崩溃了。
我怀疑是以下几行之一:
particles[i].x =( (float)rand() / (float)RAND_MAX) * SCALE ;
points[fx][fy].numb += 1;
points[fx][fy].prtcls[curn] = i;
您绝对确定所有这些数组索引值都在范围内吗?