0
votes

I am creating a matrix, then I want to fill its values with 0.

I am getting an access violation error in this line:

mat1[x][y]=0;

But I don't see why.

This is my code:

vector<float>getJenksBreaks(vector<float>uFloats,const unsigned int uNumClass)
{

    float* first(&uFloats[0]);
    float* last(first + uFloats.size());
    std::sort(first, last);

    float **mat1 = new float*[uFloats.size()];
    for (int i = 0; i < uFloats.size(); ++i) 
    {
        mat1[i] = new float[uNumClass+1];
    }
    for (unsigned long x=0;x<uNumClass+1;x++)
    {
        for (unsigned long y=0;y<uFloats.size();y++)
        {
            mat1[x][y]=0;
        }
    }

Does anybody see my error?

Thank you.

2
You're using mat[x][y] instead of mat[y][x]? - Taylor Brandstetter
Not a solution but an observation: You should strongly consider using vector<float>::iterator rather then float * for first and last, and using ufloats.begin() and uFloats.end() to find the beginning and end of the vector. - Dale Wilson
Oh, it seems you are right. Can you make your comment the reply? - tmighty
Is there some reason you are dealing with vectors as inputs and return values, yet feel the need to manage memory dynamically instead of using vectors internal to your function? - Zac Howland
@ZacHowland I don't know what you mean. I see no other way than using "new" to dynamically create a matrix whose size is not known at compilation time. - tmighty

2 Answers

1
votes

Instead of dealing with dynamic memory manually:

vector<float> getJenksBreaks(vector<float> uFloats, const unsigned int uNumClass)
{
    std::sort(uFloats.begin(), uFloats.end());

    std::vector<std::vector<float>> mat(uFloats.size());
    std::for_each(mat.begin(), mat.end() [&](std::vector<float>& v)
    {
        v.resize(uNumClass);
    });

    // ...
}

Or (even better) ...

vector<float> getJenksBreaks(vector<float> uFloats, const unsigned int uNumClass)
{
    std::sort(uFloats.begin(), uFloats.end());

    std::vector<std::vector<float>> mat(uFloats.size(), std::vector<float>(uNumClass));

    // ...
}

Which will do exactly what you did without the need to worry about proper placement of your indexes, and is much less prone to errors.

Finally, you can simply create a simple Matrix wrapper class:

class Matrix
{
public:
    Matrix(std::size_t rows, std::size_t cols) : _matrix(rows, std::vector<float>(cols))
    {

    }

    // other matrix functions

private:
    std::vector<std::vector<float>> _matrix;
};

Which would then turn your code into

vector<float> getJenksBreaks(vector<float> uFloats, const unsigned int uNumClass)
{
    std::sort(uFloats.begin(), uFloats.end());
    Matrix matrix(uFloats.size(), uNumClass);

    // ...
}
0
votes

Change these loops

for (unsigned long x=0;x<uNumClass+1;x++)
{
    for (unsigned long y=0;y<uFloats.size();y++)
    {
        mat1[x][y]=0;
    }
}

to

for (unsigned long x=0;x<uFloats.size();x++)
{
    for (unsigned long y=0;y<uNumClass+1;y++)
    {
        mat1[x][y]=0;
    }
}