1

I've written a program that works for most input, but if I ask it to make a increase precision by using a larger array (about 320x320 was when I started seeing trouble) it crashes. I searched for my issue online and found this similar problem and this tutorial on what to do about it. The problematic part of my original code is below - I had precision=320 and holepop=770.

double spacing = 2.0/(precision+1);
int lattice_int[precision][precision];
for (i=0; i<precision; ++i){
    for (ii=0; ii<precision; ++ii){
        mindist_sq = 2.0;
        lattice_int[i][ii] = 0;
        for (iii=0; iii<holepop; ++iii){
            xdist = abs(xcoord[iii] + 1.0 - spacing/2 - spacing*i);
            ydist = abs(ycoord[iii] - 1.0 + spacing/2 + spacing*ii);
            thisdist_sq = xdist*xdist+ydist*ydist;
            if (thisdist_sq < mindist_sq){
                 lattice_int[i][ii] = dint[iii];
                 mindist_sq = thisdist_sq;
            }
        }
    }
}

I tried to fix it with this change in the first two lines:

int * lattice_int;
double spacing = 2.0/(precision+1);
lattice_int = new int[precision][precision];

(I also put in "delete lattice_int[][];" at the end.) However, I received this error: 'precision' cannot occur in a constant expression Is it because I'm trying to work with multiple indices? What can I do to work around my problem? Thank you!

trincot
  • 317,000
  • 35
  • 244
  • 286
BGreen
  • 370
  • 3
  • 17
  • `int lattice_int[precision][precision];` requires `precision` to be a compile time constant. If you use `new` it does not. Has nothing to do with "multiple indices" aka. multiple dimensions. Edit: You would do `new int[precision * precision];` for the `new` version, instead of `new int[precision][precision];` as @Puppy noticed. Anyway, stick to `std::vector`, much better. – nwp Jun 26 '15 at 15:19
  • You're wrong there. If you use `new[]` then only the first dimension may be non-constant. – Puppy Jun 26 '15 at 15:19

2 Answers2

3

Don't use new[], it'll only cause you pain, suffering, memory leaks, use-after-frees, etc.

You can use std::vector in this respect.

std::vector<std::vector<int>> lattice_int(precision, std::vector<int>(precision));

No memory freeing necessary.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • Thank you! I'll have to look into the difference between vectors and arrays so I know how what to do with each. To be honest, all my programming experience is self-taught and so it has large gaps. – BGreen Jun 26 '15 at 19:53
  • @BGreen: Arrays are shit. Vectors are not. That's pretty much the main difference. – Puppy Jun 26 '15 at 20:03
0

Your lattice_int variable is 2d array. You can allocate it using following code:

int precision = 500;
int ** lattice_int;
double spacing = 2.0/(precision+1);

lattice_int = new int*[precision];
for (int i = 0; i < precision; i++)
{
    lattice_int[i] = new int[precision];
}

Same way you have to iterate for deletion of each sub array.

Note: This is pure illustration to use pointers for creating two dimensional array. The better way would be to use vector for this.

Mital Vora
  • 2,199
  • 16
  • 19
  • Thank you! I appreciate that you explained a work-around for how to do it with new[] and pointers as well, just to show how to do it by thinking a little out of the box. – BGreen Jun 26 '15 at 19:54
  • @BGreen: The box is there for a very important reason. It's because if you stand outside it, you will step on a landmine. Or a hundred. And then your application will be an unreliable buggy piece of crap. – Puppy Jun 26 '15 at 20:04
  • On the other hand if I never left the box I'd still be at "Hello World." There's a fine line somewhere between trying new things and trying to use what you don't understand. I get your point, though - you're saying not to use tools in ways that they aren't suitable to be used.I was just trying to show some appreciation for another perspective, even if it was in practice a bad idea. – BGreen Jun 26 '15 at 21:36