[英]Segmentation fault 11 with my C program
我的程序目前有问题。 我遇到了细分错误11,但不知道为什么。 奇怪的是,有时它可以正常工作,但是大多数时候我都会收到此错误。 当我删除此部分时: /* set to inital conditions */ *(*(bounds + 0) +0) = x; *(*(bounds + 0) +1) = x; *(*(bounds + 1) +0) = y; *(*(bounds + 1) +1) = y; *(*(bounds + 2) +0) = z; *(*(bounds + 2) +1) = z;
/* set to inital conditions */ *(*(bounds + 0) +0) = x; *(*(bounds + 0) +1) = x; *(*(bounds + 1) +0) = y; *(*(bounds + 1) +1) = y; *(*(bounds + 2) +0) = z; *(*(bounds + 2) +1) = z;
从代码是否正常工作,所以我认为这是问题所在。 当我只通过循环运行一次(minIter = maxIter)时,它正在工作。 如果有人可以帮助我,我将不胜感激。
/* creating 2D Array for the iteration points*/
double **iterationPoints;
/* creating 2D Array for the normalized points*/
double **normalizedPoints;
/* creating 3D Array for the grid*/
bool ***grid;
/* creating 2D Array for the min/max of attractor in all directions*/
double **bounds;
/* setting up loop, to create data for increasing iterations*/
/* open/create file for data */
FILE *file = fopen("LorentzIterationData.dat", "w");
if (file == NULL)
{
printf("Error opening file!\n");
exit(1);
}
/* setting parameters for loop */
int minIter = 1000, maxIter = 10000;
int stepSizeIter = 100;
int iterations;
for(iterations = minIter; iterations <= maxIter; iterations += stepSizeIter){
/* create bound array */
bounds = (double **) malloc(3 *sizeof(double *));
int i;
for(i = 0; i < 2; i++){
bounds[i] = (double *) malloc(2 *sizeof(double));
}
/* set to inital conditions */
*(*(bounds + 0) +0) = x; *(*(bounds + 0) +1) = x;
*(*(bounds + 1) +0) = y; *(*(bounds + 1) +1) = y;
*(*(bounds + 2) +0) = z; *(*(bounds + 2) +1) = z;
/* calculate iterationPoints */
iterationPoints = (double **) malloc(iterations *sizeof(double *));
for(i = 0; i < iterations; i++){
iterationPoints[i] = (double *) malloc(3 *sizeof(double));
}
calcuTraj(iterationPoints,a,b,c,x,y,z,h,iterations, bounds);
/* normalize Data */
normalizedPoints = (double **) malloc(iterations *sizeof(double *));
for(i = 0; i < iterations; i++){
normalizedPoints[i] = (double *) malloc(3 * sizeof(double));
}
normalize(iterationPoints, normalizedPoints, bounds, iterations);
/* creating 3D Array for the grid of boxes in space*/
/* setting minimum for sidelength of the grid */
double minGridSideLength = 1;
/* calculating array size */
int boxesPerDim = ceil(minGridSideLength/epsion) +1;
printf("boxesPerDim: %d \n", boxesPerDim);
/* create grid array */
grid = (bool ***) malloc(boxesPerDim *sizeof(bool **));
int j_X, j_Y;
for(j_X = 0; j_X < boxesPerDim; j_X++){
grid[j_X] = (bool **) malloc(boxesPerDim *sizeof(bool *));
for(j_Y = 0; j_Y < boxesPerDim; j_Y++){
grid[j_X][j_Y] = (bool *) calloc(boxesPerDim,sizeof(bool *));
}
}
/* count hitted boxes */
printf("boxesHitted: %d \n", boxCount(normalizedPoints, grid, iterations, epsion, boxesPerDim));
/* free storage */
free(iterationPoints);
free(grid);
free(bounds);
free(normalizedPoints);
}
您分配3个指针的数组
bounds = (double **) malloc(3 *sizeof(double *));
但只初始化其中两个
for(i = 0; i < 2; i++){
bounds[i] = (double *) malloc(2 *sizeof(double));
}
您不会分配从最初的bounds
分配中获得的每个后续double*
。
在这里为3个double*
类型创建空间:
bounds = (double **) malloc(3 *sizeof(double *));
但你只能从0到1循环时malloc
每个后续荷兰国际集团空间double*
指针:
int i;
for (i = 0; i < 2; i++){
bounds[i] = (double *) malloc(2 *sizeof(double));
}
这样就使得*(*(bounds + 2) +0)
和*(*bounds + 2) +1)
取消引用了尚未初始化的double*
。 这条线
*(*(bounds + 2) +0) = z; *(*(bounds + 2) +1) = z;
调用未定义的行为,有时会导致您看到的段错误。
从i=0
循环到i<3
,以便为每个double*
分配足够的空间double*
for (i = 0; i < 3; i++){
bounds[i] = (double *) malloc(2 *sizeof(double));
}
这是关于为什么不在代码中使用幻数的展览A。 如果您使用了某种#define BOUNDS_LENGTH 3
常量并对其进行了循环,那么这将不是问题。 此外,没有必要为此应用程序使用动态分配的内存。 也许这只是您创建的MCVE,但是如果您知道在编译时需要多少空间,则没有理由动态分配它,除非您需要“大量”内存。 只需声明double bounds[3][2];
,或者更好
#define BOUNDS_LENGTH 3
#define BOUNDS_ITEM_LENGTH 2
....
double bounds[BOUNDS_LENGTH][BOUNDS_ITEM_LENGTH];
本来可以避免malloc
问题,并且可以使后续遍历数组的循环更清晰,更不易出错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.