c++ program won't work

Associate
Joined
12 May 2005
Posts
1,777
MY program won't work, there are no compilation errors and I get a stack overflow error while debugging. Its a very simple program that finds line intersections from a huge list of co-ordinates. Any ideas as to why it fails would be very helpful.
I've find out that these variables are causing the problem, but they are vital to the advanced part of the program:
// double xvalues[1001] = {0};
// double yvalues[1001] = {0};
// double x1values[1001] = {0};
// double y1values[1001] = {0};
double gradients[1001] = {0};
double constants[1001] = {0};
// double intersects[1001][1001][1] = {0};
 
Last edited:
What are you compiling this with?
I've just tried the second file with Dev-C++, and it runs without a stack overflow:
....
0.108147 0.896901 -1.10786 0.972156
0.565962 0.826784 0.58803 1.00928
0.613659 0.961498 0.590788 1.59011
The longest line is 2.09998
The shortest line is 1.41421e-007

And going back to the first, commenting out the "double intersects[1001][1001][1] = {0};" stops it crashing for me.
Looks like it isn't happy allocating 8*1001*1001=8016008 bytes (~7.6MB).
So I guess you're going to have to dynamically allocate the memory, which I'm not sure how you do in C++ yet.
 
Visual c++ doesn't like my programs, the compiler crashes most of the time.
You dynamically allocate arrays by doing this:
myarray = new double[3][5]; But if I try this:
double myarray[2][2];
myarray = new double [2]; I get all kinds of strange errors regarding converting [*] to [*][2] and something about pointers which I have never seen the point of using.
 
The reason you're getting the error is because you're trying to put huge amounts of data on the stack. All your variables are being declared local to their functions -- you can fix this by making them file static, which will cause the compiler/linker to put them on the heap.

So, based on the first file I'd suggest something that looks like:

Code:
...
static double xvalues[1001] = {0};
static double yvalues[1001] = {0};
static double x1values[1001] = {0};
static double y1values[1001] = {0};
static double gradients[1001] = {0};
static double constants[1001] = {0};
static double intersects[1001][1001][1] = {0};
static bool intersect[1001][1001] = {false};
...
int main()
{
...
}
...
Hope that makes a bit of sense. As has been stated already, you could probably also get C++ to dynamically allocate space for these on the heap:
Code:
...
int main()
{
...
   double *xvalues = new double[1001];
   double *yvalues = new double[1001];
   double *x1values = new double[1001];
   double *y1values = new double[1001];
   double *gradients = new double[1001];
   double *constants = new double[1001];
   double (*intersects)[1001][1] = new double[1001][1001][1];
   bool  (*intersect)[1001] = new bool[1001][1001];

...
}
...
 
Back
Top Bottom