简体   繁体   中英

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. 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; 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. 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

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 .

here you create space for 3 double* types:

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

but then you only loop from 0 to 1 when malloc ing space for each subsequent double* pointer:

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. 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*

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. If you had used some kind of #define BOUNDS_LENGTH 3 constant and looped over that, this wouldn't have been a problem. 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. Simply declaring 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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