使用我的C程序进行分段故障11

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用我的C程序进行分段故障11相关的知识,希望对你有一定的参考价值。

我目前的程序有问题。我得到了一个分段错误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;从它正在运行的代码,所以我认为这是问题。当我只是通过循环运行一次(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!
");
  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 
", 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 
", boxCount(normalizedPoints, grid, iterations, epsion, boxesPerDim));

/* free storage */
free(iterationPoints);
free(grid);
free(bounds); 
free(normalizedPoints);

}
答案

你没有分配你从double*的初始分配中获得的每个后续bounds

在这里你为3个double*类型创建空间:

bounds = (double **) malloc(3 *sizeof(double *));

但是当你为每个后续的malloc指针添加double*ing空间时,你只能从0循环到1:

int i;
for (i = 0; i < 2; i++){
    bounds[i] = (double *) malloc(2 *sizeof(double));
}

所以这使得*(*(bounds + 2) +0)*(*bounds + 2) +1)解除引用double*s尚未初始化。这条线

*(*(bounds + 2) +0) = z; *(*(bounds + 2) +1) = z;

调用未定义的行为,有时会导致您看到的段错误。

i=0循环到i<3,以便为每个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问题,并使任何后续循环通过阵列更清晰,更不容易出错。

另一答案

你分配一个3指针的数组

bounds = (double **) malloc(3 *sizeof(double *));

但只初始化其中的2个

for(i = 0; i < 2; i++){
    bounds[i] = (double *) malloc(2 *sizeof(double));
}

以上是关于使用我的C程序进行分段故障11的主要内容,如果未能解决你的问题,请参考以下文章

带有地图和矢量c ++的分段故障11

GCC 分段故障 Mac

分段故障核心转储错误

在 C 中转储的分段故障核心

MPI 分段故障(信号 11)

分段故障核心转储...?