执行完所有事件后出现分段错误。我尝试了几个小时,但仍然找不到问题。我感到沮丧和任何援助表示赞赏!程序结束时出现分段错误C
所有代码位于此链接的目录“KNN-GitHub”下:https://ide.c9.io/captainzidane/kd_tree 要使用point.txt,test2.c,kdtree.c,kdtree.h。
这是我的“test.c”文件的一部分,其中seg故障发生在最后一行。也就是说,最后的大括号}
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include "kdtree.h"
#define DEF_NUM_PTS 10
#define MAX_NUM_OF_POINTS 20000
#define DIMENSION 3
int main (int argc, char *argv[]) {
//int arraySize;
double pointArray[MAX_NUM_OF_POINTS][DIMENSION];
FILE *coordFile = fopen ("points.txt", "r");
//coord *coords = malloc(sizeof(coord) * MAX_NUM_OF_POINTS);
int lineCounter = 0;
while (fscanf (coordFile, "%lf %lf %lf", &pointArray[lineCounter][0], &pointArray[lineCounter][1],
&pointArray[lineCounter][2]) == 3 && lineCounter < MAX_NUM_OF_POINTS) {
lineCounter++;
}
double range = 8.1;
int num_of_nearest;
double queryPoint[DIMENSION];
int counter;
int dist;
int num;
//scanf ("%lf %lf %lf %d", &queryPoint[0], &queryPoint[1], &queryPoint[2], &num);
queryPoint[0] = 1;
queryPoint[1] = 5;
queryPoint[2] = 8;
num = 10;
//Build and insert points to a KD Tree
struct kdtree *newTree = buildTree (pointArray);//////make it run only once
//Return nearst N nodes and distance in order from smallest to largest
struct knn_result *res_n = knn(newTree, queryPoint, num);
free (res_n);
printf ("======================================================\n");
//Return nodes within a range of the query point
struct nn_range *res_range = nn_range(newTree, queryPoint, range);
free (res_range);
printf ("======================================================\n");
fclose (coordFile);
kd_free(newTree);///To be deleted
return EXIT_SUCCESS;
}
这里是我的 “kdtree.c” 文件的一部分包含可能导致错误的功能:
struct nn_range {
double nearestRange[MAX_NUM_OF_POINTS][DIMENSION+1];
int nPoints;
};
struct nn_range *nn_range (struct kdtree *tree, const double *queryPoint, double range)
{
printf ("Nodes within a radius of %.3f to the query point:\n", range);
printf ("-------------------------------------------------\n");
struct kdres *presults_range = kd_nearest_range(tree, queryPoint, range);
double temp[3] = {0};
double dist_temp = 0;
struct nn_range *res = malloc(sizeof(struct nn_range));
int counter = 0,i;
res->nPoints = kd_res_size(presults_range);
printf("found %d results within the radius of %.3f\n", res->nPoints, range);
while(!kd_res_end(presults_range) && counter < res->nPoints)
{
dist_temp = 0;
/* get the data and position of the current result item */
kd_res_item(presults_range, temp);
/* compute the distance of the current result from the pt */
for(i=0; i<DIMENSION; i++)
{
dist_temp += SQ(queryPoint[i] - temp[i]);
}
dist_temp = sqrt(dist_temp);
res->nearestRange[counter][0] = temp[0];
res->nearestRange[counter][1] = temp[1];
res->nearestRange[counter][2] = temp[2];
res->nearestRange[counter][3] = dist_temp;
/* print out the retrieved data */
printf("node at (%.3f, %.3f, %.3f) is %.3f away\n", res->nearestRange[counter][0],
res->nearestRange[counter][1],
res->nearestRange[counter][2],
res->nearestRange[counter][3]);
/* go to the next entry */
kd_res_next(presults_range);
counter++;
}
kd_res_free(presults_range);
return res;
}
GDB说
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400ab9 in main (argc=1, argv=0x7fffffffe1d8) at test2.c:117
117 }
Valgrind says
==5369== Jump to the invalid address stated on the next line
==5369== at 0x3FE970FD6F3D35D2: ???
==5369== by 0x3FED48EE17391B6D: ???
==5369== by 0xFFF000157: ???
==5369== by 0xFFFFFFFF: ???
==5369== by 0x4008BC: ??? (in /home/ubuntu/workspace/KNN - GitHub/c)
==5369== Address 0x3fe970fd6f3d35d2 is not stack'd, malloc'd or (recently) free'd
==5369==
==5369==
==5369== Process terminating with default action of signal 11 (SIGSEGV)
==5369== Bad permissions for mapped region at address 0x3FE970FD6F3D35D2
==5369== at 0x3FE970FD6F3D35D2: ???
==5369== by 0x3FED48EE17391B6D: ???
==5369== by 0xFFF000157: ???
==5369== by 0xFFFFFFFF: ???
==5369== by 0x4008BC: ??? (in /home/ubuntu/workspace/KNN - GitHub/c)
==5369==
==5369== HEAP SUMMARY:
==5369== in use at exit: 0 bytes in 0 blocks
==5369== total heap usage: 40,057 allocs, 40,057 frees, 2,561,888 bytes allocated
==5369==
==5369== All heap blocks were freed -- no leaks are possible
==5369==
==5369== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==5369==
==5369== 1 errors in context 1 of 1:
==5369== Jump to the invalid address stated on the next line
==5369== at 0x3FE970FD6F3D35D2: ???
==5369== by 0x3FED48EE17391B6D: ???
==5369== by 0xFFF000157: ???
==5369== by 0xFFFFFFFF: ???
==5369== by 0x4008BC: ??? (in /home/ubuntu/workspace/KNN - GitHub/c)
==5369== Address 0x3fe970fd6f3d35d2 is not stack'd, malloc'd or (recently) free'd
==5369==
==5369== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault
这里是输出的一部分
Nodes within a radius of 8.100 to the query point:
-------------------------------------------------
found 24 results within the radius of 8.100
node at (0.718, 0.958, 0.994) is 8.093 away
node at (0.716, 0.974, 0.992) is 8.087 away
node at (0.629, 0.960, 0.991) is 8.099 away
node at (0.629, 0.960, 0.991) is 8.099 away
node at (0.556, 0.994, 0.979) is 8.096 away
node at (0.677, 0.985, 0.991) is 8.084 away
node at (0.556, 0.994, 0.979) is 8.096 away
node at (0.677, 0.985, 0.991) is 8.084 away
node at (0.909, 0.966, 0.980) is 8.097 away
node at (0.909, 0.966, 0.980) is 8.097 away
node at (0.909, 0.966, 0.980) is 8.097 away
node at (0.909, 0.966, 0.980) is 8.097 away
node at (0.978, 0.958, 0.993) is 8.089 away
node at (0.978, 0.958, 0.993) is 8.089 away
node at (0.978, 0.958, 0.993) is 8.089 away
node at (0.978, 0.958, 0.993) is 8.089 away
node at (0.857, 0.970, 0.978) is 8.098 away
node at (0.862, 0.985, 0.984) is 8.085 away
node at (0.862, 0.985, 0.984) is 8.085 away
node at (2.000, 2.000, 2.000) is 6.782 away
node at (2.000, 2.000, 2.000) is 6.782 away
node at (1.000, 1.000, 1.000) is 8.062 away
node at (1.000, 1.000, 1.000) is 8.062 away
node at (1.000, 2.000, 2.000) is 6.708 away
======================================================
Segmentation fault
您好!您是否已经检查'counter'是否总是小于'MAX_NUM_OF_POINTS'? – RCaetano
...什么是'DIMENSION'值? – LPs
是的,我用gdb info locals来仔细检查。计数器在这种情况下大约为25,MAX_NUM_OF_POINTS是20000. DIMENSION是3,这意味着点在3D空间中。 –