Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:39:38+00:00 2026-06-15T08:39:38+00:00

I have a written a C program to find the k nearest neighbors of

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:39:39+00:00Added an answer on June 15, 2026 at 8:39 am

    I think you’re exceeding your available stack (see the MSDN docs on _chkstk). Try allocating the arrays dynamically instead

    int main()
    {
        double* points = malloc(sizeof(double) * MAX_SIZE*2];
        double* result = malloc(sizeof(double) * MAX_SIZE*3*2];
        ...
        free(points);
        free(result);
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a program to find all the possible permutations of a given
I have written a Python program to find the carrier of a cell phone
Hey all. I have written a program that sequentially scans certain parts of a
I have written the following program using Parallel Haskell to find the divisors of
Suppose I have a program written in C/C++ and I'd like to find out
I have a program to find the reciprocal of a number but the main
I have written a program based on trying to solve the Knight’s Tour problem.
I have this program written in C++ Builder 6. I didn't write all the
I have a program written in Ruby that uses multiple levels of inheritance. All
I have written a program in C running on UNIX which counts the number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.