简体   繁体   English

我的C程序出现分段错误11

[英]Segmentation fault 11 with my C program

I am having a problem with my program at the moment. 我的程序目前有问题。 I am getting an segmentation fault 11, but I have no idea why. 我遇到了细分错误11,但不知道为什么。 The strange thing is, that sometimes it is working, but most of the time I am getting this error. 奇怪的是,有时它可以正常工作,但是大多数时候我都会收到此错误。 When I removed this part: /* 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; /* 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; from the code it is working, so I think here is the problem. 从代码是否正常工作,所以我认为这是问题所在。 When i just run for one time trough the loop (minIter = maxIter) it is working. 当我只通过循环运行一次(minIter = maxIter)时,它正在工作。 I would be thankful if somebody could help me. 如果有人可以帮助我,我将不胜感激。

/* 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);

}

You allocate an array of 3 pointers 您分配3个指针的数组

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

but initialize only 2 of them 但只初始化其中两个

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

You're not allocating each subsequent double* that you get from your initial allocation of bounds . 您不会分配从最初的bounds分配中获得的每个后续double*

here you create space for 3 double* types: 在这里为3个double*类型创建空间:

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

but then you only loop from 0 to 1 when malloc ing space for each subsequent double* pointer: 但你只能从0到1循环时malloc每个后续荷兰国际集团空间double*指针:

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

so that makes *(*(bounds + 2) +0) and *(*bounds + 2) +1) dereference double* s which have not been initialized. 这样就使得*(*(bounds + 2) +0)*(*bounds + 2) +1)取消引用了尚未初始化的double* This line 这条线

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

invokes undefined behavior, which sometimes results in a segfault as you're seeing. 调用未定义的行为,有时会导致您看到的段错误。

Loop from i=0 to i<3 in order to allocate enough space for each double* i=0循环到i<3 ,以便为每个double*分配足够的空间double*

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

This is exhibit A on why not to use magic numbers in code. 这是关于为什么不在代码中使用幻数的展览A。 If you had used some kind of #define BOUNDS_LENGTH 3 constant and looped over that, this wouldn't have been a problem. 如果您使用了某种#define BOUNDS_LENGTH 3常量并对其进行了循环,那么这将不是问题。 Furthermore, there's no point in using dynamically allocated memory for this application. 此外,没有必要为此应用程序使用动态分配的内存。 Perhaps this is just an MCVE you created, but if you know how much space you need at compile time, there's no reason to dynamically allocate it unless you need "a lot" of memory. 也许这只是您创建的MCVE,但是如果您知道在编译时需要多少空间,则没有理由动态分配它,除非您需要“大量”内存。 Simply declaring double bounds[3][2]; 只需声明double bounds[3][2]; , or better yet ,或者更好

#define BOUNDS_LENGTH 3
#define BOUNDS_ITEM_LENGTH 2

....

double bounds[BOUNDS_LENGTH][BOUNDS_ITEM_LENGTH];

would have avoided the malloc problems, and would make any subsequent looping through the array more clear and less error-prone. 本来可以避免malloc问题,并且可以使后续遍历数组的循环更清晰,更不易出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM