I have a written a C program to find the k nearest neighbors of all the points in a given set of points (randomly generated). The problem is when I increase the number of points(and consequently the array size) to 10000 the program gives segment violation error as soon as I call the function to find out the nearest neighbors. I am not able to get inside the function using the debugger. As soon as I do a “Step Into” the program crashes.
I have used code-blocks and Eclipse CDT (on Windows 7) and both give the error at the same point. In case of code-blocks it gives segment violation and in case of Eclipse it first shows – “No source available for __chkstk_ms() at 0x4039a7” and then the error comes from the OS itself – “KNN.exe has stopped working”
However the program runs fine on Linux(Ubuntu 32bit).
Here is the code snippet :
#define MAX_SIZE 10000
int main()
{
int n = MAX_SIZE;
int k = 3;
int i;
double points[MAX_SIZE*2]; //2-D array in row-major order
double result[MAX_SIZE*3*2];
srand(time(NULL));
for(i=0; i < n; i++)
{
points[i*2] = (double)rand()/(double)RAND_MAX;
points[i*2 + 1] = (double)rand()/(double)RAND_MAX;
}
seek(points,n,k,result); //<---------- ERROR
seek(points,n,k,result); //<------------ NO ERROR
....
}
void seek(const double * const points, int n, int k, double *result)
{
TreeNode qtree[MAX_SIZE];
int order_array[MAX_SIZE];
int num_nodes = build_quadtree(a, n, k, qtree,order_array);
......
}
struct tree_node
{
int id;
int num_points;
int start_order;
int end_order;
int parent;
int child[4];
struct rectangle rect;
enum boolean is_leaf;
};
struct point
{
double x;
double y;
};
struct rectangle
{
int id;
double xmin,xmax,ymin, ymax;
struct point midpt;
};
What is more confusing is that I have another function with the same arguments which is running without any problem.
Please provide suggestions on how to debug this.
EDIT:- . I have posted the first few lines of seek() function. As the replies have pointed out I am actually allocating a lot of memory on the seek function but I am wondering why it is not a problem in linux.
I think you’re exceeding your available stack (see the MSDN docs on _chkstk). Try allocating the arrays dynamically instead
The stack overflow is happening when you call
seek. You haven’t posted code for it but may have to rework it also to reduce its stack use.