2

I'm just learning about C++. I'm doing practice with initialize list, so I made a class like this

class Matrix
{
    public:
        const int x_size;
        const int y_size;
        int *data;

        Matrix(int _x_size, int _y_size) : x_size(_x_size), y_size(_y_size)
        {
            data = new int[y_size][x_size];
        }

        ~Matrix()
        {
            delete[][] data;
        }
};

int main(void)
{
    Matrix A = Matrix(10, 10);
    return 0;
}

And compiler said as: array size in operator new must be constant. So I searched and someone said, these are not 'compiler time constant'.

But it is obvious that I can't use that size as macros in here... Then. How should I get proper-sized array with Constructor?

3
  • 3
    You should really be using a single, 1D array, and add access via two indices. The 1D array could be an std::vector<int> to simplify things. Commented Jul 7, 2014 at 6:31
  • 2
    Forget about arrays, use std::vector. Commented Jul 7, 2014 at 6:36
  • The deletion would be delete[] data; btw - the delete[] form covers arrays of any dimension. Commented Jul 7, 2014 at 6:41

2 Answers 2

4

If you are just learning c++ then the best tip is stay away from memory management. Use the stl types if you can. Use a std::vector to replace that array:

std::vector<std::vector<int>> data;

create it like this:

data(y_size, std::vector<int>(x_size, 0));

and access it like this:

data[i][j];

As jaunchopanza said, you can also use a 1D array which might be better. You would create and edit it in similar ways:

std::vector<int> data;
data(y_size * x_size, 0);
data[y_size*i + j];

The advantage is that it is faster for accessing, especially if x_size and y_size are going to be large. There is also an advantage in that the vector of vectors may be stored all over the place, as in each row(or column) will be in different places in memory. If you intend to get data that overlaps more than one row (or column) then it would be better to use the 1D array for speed.

You can Find more info here: http://en.cppreference.com/w/cpp/container/vector

If you want to turn this into a matrix and do math etc. Than I would highly recomend Eigen it is by far the best matrix library: http://eigen.tuxfamily.org/index.php?title=Main_Page

Sign up to request clarification or add additional context in comments.

11 Comments

Downvoter, care to add a comment explaining so i can make my answer better?
OP seems to be attempting to get one block of data, which is the right thing to do. A vector of vectors is a bunch of fragmented data blocks. It doesn't seem like the best way to represent a standard N*M matrix.
@juanchopanza Yep i think you may have just seen that I edited it as well.
IMHO there's no "best" way to represent a matrix; it depends on what you're going to do with it. A vector of vectors has some advantages and some disadvantages.
@MattMcNabb I agree, i mean the vector of vectors is easier to read for one.
|
-2

you should write sth like this ( although it is not exception safe)

class Matrix
{
    public:
        const int x_size;
        const int y_size;
        int ** data;

        Matrix(int _x_size, int _y_size) : x_size(_x_size), y_size(_y_size)
        {
            data = new int* [y_size];
            for(int i =0 ; i < y_size ; i++)
            {
                data[i] = new int[x_size] ;
            }
        }

        ~Matrix()
        {
            for(int i= 0 ; i < y_size ; i++)
            {
                delete [] data[i];
            }
            delete[] data;
        }
};

4 Comments

This violates the Rule of Three, and exception-safety; and even if you fix both of those , it is a huge amount of code bloat compared to the one-line option of using a vector of vectors.
this code is for educational purpose to fix his original idea and yeah it is not exception safe :)
The fact that this is for educational purposes is exactly why this is a bad answer: We should teach C++ newbbies about resorce management, raii, value semantics, etc; not manual memory management
@Manu343726 I don't agree with you although using raii and resource management is highly recommended , they are not always available in all compilers and systems i think that the excerpt of code i wrote should be familiar to every c++ programmer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.