samedi 24 mars 2018

How to correctly ensure I'm not accessing elements out of a matrix?

I'm having problems taking care of forbidden memory access. I have a task to find the highest mountain of a matrix (defined as a number that is higher than the sum of square of numbers surrounding it). Code works when I hard-code it not to access elements out of a matrix, which is not a good thing at all.

How do I make sure I never try to access memory that I'm not supposed to?

typedef std::vector<std::vector<double>> Matrix;
typedef std::vector<double> Vector;
unsigned CalculateSum(Matrix mat, int counter, int x, int y)
{
unsigned sum(0);
for (int i(x - counter); i <= (x + counter); i++) {
    for (int j(y - counter); j <= (y + counter); j++) {
        if (i == x && j == y) continue;
        sum += mat.at(i).at(j);
    }
}
return sum;
}
Matrix HighestMountain(Matrix mat) 
{
for (int i(0); i < mat.size(); i++) {
    if (mat.at(i).size() != mat.size()) throw std::domain_error("Wrong 
format");
}
Matrix insert;
Vector v;
int sum1(0), sum2(0);
int counter(1);
for (int i(0); i < mat.size(); i++) {
    for (int j(0); j < mat.at(i).size(); j++) {
        if (i == 0 || i == mat.size() - 1 || j == 0 || j == mat.at(i).size() 
- 1) continue;
        while (CalculateSum(mat, counter, i, j) < mat.at(i).at(j)) {
            insert.clear();
            for (int k(i - counter); k <= counter + 2; k++) {
                for (int l(j - counter); l <= counter + 2; l++) {
                    v.push_back(mat.at(k).at(l));
                }
                insert.push_back(v);
                v.clear();
            }
            counter++;
        }
    }
}
return insert;
}

Aucun commentaire:

Enregistrer un commentaire